c# - Weird "Random" behaviour with Random -
this question has answer here:
on again i´m needing here, today doin c# homework consist in creating method can sum result of 2 dice rolls, far, good!, no hard thing do... but, when run code odd thing happens, let code , results speak them self:
using system; class program { public static string gettimestamp() { datetime savenow = datetime.now; return savenow.tostring("yyyymmddhhmmssffff"); } public static int throwdice() { int randval1, randval2; string ts1, ts2; random rand = new random(); randval1 = rand.next(1,6); ts1 = gettimestamp(); randval2 = rand.next(1,6); ts2 = gettimestamp(); console.writeline("dice 1: \t" + randval1 + ' ' + ts1); console.writeline("dice 2: \t" + randval2 + ' ' + ts2); console.writeline("-----------------"); return randval1 + randval2; } static void main() { for(int = 1; i<=10;i++) { console.writeline("dice roll #" + i); console.writeline("dice sum:\t" + throwdice()); console.writeline(); } } }
now, got when ran it:
dice roll #1
dice 1: 1 / timestamp : 201308260146259075
dice 2: 2 / timestamp : 201308260146259131
dice sum: 3
dice roll #2
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
dice roll #3
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
dice roll #4
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
dice roll #5
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
dice roll #6
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
dice roll #7
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
dice roll #8
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
dice roll #9
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
dice roll #10
dice 1: 2 / timestamp : 201308260146259135
dice 2: 5 / timestamp : 201308260146259135
dice sum: 7
-roll 1 perfect
-roll 2/dice 1... perfect,
-roll 2/dice 2... wth???
those results weird, expecting different timestamps , different values on each roll. timestamp stuff idea came check time when numbers generated, , looks both "dices" rolled @ same time, looks "random" generates seed using clock.
i tryed creating whole class dice diceroll() method, there both complete different objects getting same results on , over..
how can fix this?, ideas?
declare rand static variable , initilize once
using system; class program { public static string gettimestamp() { datetime savenow = datetime.now; return savenow.tostring("yyyymmddhhmmssffff"); } public static int throwdice() { int randval1, randval2; string ts1, ts2; randval1 = rand.next(1,7); ts1 = gettimestamp(); randval2 = rand.next(1,7); ts2 = gettimestamp(); console.writeline("dice 1: \t" + randval1 + ' ' + ts1); console.writeline("dice 2: \t" + randval2 + ' ' + ts2); console.writeline("-----------------"); return randval1 + randval2; } static random rand ; static void main() { rand = new random(); for(int = 1; i<=10;i++) { console.writeline("dice roll #" + i); console.writeline("dice sum:\t" + throwdice()); console.writeline(); } } }
and time stamps can identical few rolls not many strage
Comments
Post a Comment