469,950 Members | 2,386 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Random NOt random?



I do the following in code

int count = randata.ProductTable.Rows.Count;

Random r = new Random();

Random r2 = new Random();

int i = r.Next(1,count);

int x = r2.Next(1,count);

and i and x will always be the same....

how does that work?
Nov 18 '05 #1
3 1094
Have you run r.Random() and r2.Random before?

Certainly you have to initialize random number generator before use it.

Note that if you want to use Random() function instead of the object, you
still need to run Randomize statement first.

Regards,
Lau Lei Cheong

"Darren Clark" <dc******@hotmail.com> bl
news:%2****************@TK2MSFTNGP09.phx.gbl g...


I do the following in code

int count = randata.ProductTable.Rows.Count;

Random r = new Random();

Random r2 = new Random();

int i = r.Next(1,count);

int x = r2.Next(1,count);

and i and x will always be the same....

how does that work?

Nov 18 '05 #2
I read somewhere that Random is only random if a subsequent call is a
certain amount of time after. Something to do with it being linked to the
clock cycle of which the lowest granularity is ms and as you can do both
calls in one cycle you get teh same number. Put a Sleep() inbetween to
force a context switch and see what happens then.

MattC
"Darren Clark" <dc******@hotmail.com> wrote in message
news:#4**************@TK2MSFTNGP09.phx.gbl...


I do the following in code

int count = randata.ProductTable.Rows.Count;

Random r = new Random();

Random r2 = new Random();

int i = r.Next(1,count);

int x = r2.Next(1,count);

and i and x will always be the same....

how does that work?

Nov 18 '05 #3
Darren Clark wrote:
I do the following in code

int count = randata.ProductTable.Rows.Count;

Random r = new Random();

Random r2 = new Random();

int i = r.Next(1,count);

int x = r2.Next(1,count);

and i and x will always be the same....


The Random class's generator depends entirely on the seed used to
initialize it - 2 generator instance initialized with the same seed will
produce the exact same sequence. At times this is what you want (for
example, when testing you might want to be able to reproduce results).

The Random() constructor you're calling uses the system clock as a seed.
Since the 2 calls are so close together in time it's very, very likely
that they will have the same seed.

See the docs for Random( int) for information on how to avoid this problem.

Also, the Random class is not a particularly good RNG - it's good enough
maybe for games and simple simulations, but if you want a really good
set of random bits look at the RNGCryptoServiceProvider class.

--
mikeb
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Paul Rubin | last post: by
6 posts views Thread by Acacia | last post: by
10 posts views Thread by Virus | last post: by
10 posts views Thread by Johnny Snead | last post: by
10 posts views Thread by Marshall Belew | last post: by
15 posts views Thread by Steven Macintyre | last post: by
4 posts views Thread by tshad | last post: by
8 posts views Thread by Daniel | last post: by
6 posts views Thread by Mike Langworthy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.