By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

Random not really random...

P: n/a
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.Now.Ticks;
Random rndNum = new Random(TheSeed);
RandNum = rndNum.Next(0, TotalRows);
Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" + TotalRows);

My page gets called everytime and these are the values that I'm getting for
TotalRows = 8

RandNum:1 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:7 Low:0 High:8

Does anyone know why C# Random function is generating more 1,0,2,3 much more
frequently than 4,5,6,7,8?

Thank you
Maziar A.
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
No computer can generate a truly random number on its own.
In addtion, the Math.Random class is not as random as it could be.
For better results, try using the
System.Security.Cryptography.RandomNumberGenerator class

Here's more info:
http://msdn.microsoft.com/library/de...classtopic.asp

http://www.dotnet247.com/247referenc...31/158262.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:QC*******************@news04.bloor.is.net.cab le.rogers.com...
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.Now.Ticks;
Random rndNum = new Random(TheSeed);
RandNum = rndNum.Next(0, TotalRows);
Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" + TotalRows);
My page gets called everytime and these are the values that I'm getting for TotalRows = 8

RandNum:1 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:7 Low:0 High:8

Does anyone know why C# Random function is generating more 1,0,2,3 much more frequently than 4,5,6,7,8?

Thank you
Maziar A.

Nov 18 '05 #2

P: n/a
Random means random. Roll a dice six times, and see what the spread is.
Chances are, at least one number will come up at least twice, and at least
one other won't come up at all. Random values only spread out evenly when
the number of statistics is very high.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:QC*******************@news04.bloor.is.net.cab le.rogers.com...
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.Now.Ticks;
Random rndNum = new Random(TheSeed);
RandNum = rndNum.Next(0, TotalRows);
Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" + TotalRows);
My page gets called everytime and these are the values that I'm getting for TotalRows = 8

RandNum:1 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:0 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:1 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:3 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:2 Low:0 High:8
RandNum:7 Low:0 High:8

Does anyone know why C# Random function is generating more 1,0,2,3 much more frequently than 4,5,6,7,8?

Thank you
Maziar A.

Nov 18 '05 #3

P: n/a
"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:QC*******************@news04.bloor.is.net.cab le.rogers.com...
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.Now.Ticks;
Random rndNum = new Random(TheSeed); The above should be done one time only. The following lines should be
iterated. RandNum = rndNum.Next(0, TotalRows);
Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" + TotalRows);
Does anyone know why C# Random function is generating more 1,0,2,3 much more frequently than 4,5,6,7,8?

For starters your random seed doesn't look to be very random. Are you
creating/destroying the rndNum object for every number? If so then you
aren't making use of a randomizing algorithm. Every number will be the
first in the sequence based on the supplied seed. If you call it twice with
the same seed (i.e. within one tick) you will get the same number. You
might notice your numbers repeat often, a sure sign you are within one tick
on two successive iterations (this would cause a failure on a runs test for
randomness.) The distribution of random numbers over your range will be
very poor (as you have seen) and a chi-square statistical test won't do too
well either to test your set of numbers for a large number of iterations.
Try generating the same sequence, but only calling the .Next method inside
your loop and preserve the rndNum object without resetting the seed each
time.
Jack Peacock
Nov 18 '05 #4

P: n/a
Thanks for your feedback. This is a banner distribution application. So
it's called everytime the page is called. How can I call the .Next method
inside my loop more than once?

Thanks
Maz.
"Jack Peacock" <pe*****@simconv.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:QC*******************@news04.bloor.is.net.cab le.rogers.com...
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.Now.Ticks;
Random rndNum = new Random(TheSeed);

The above should be done one time only. The following lines should be
iterated.
RandNum = rndNum.Next(0, TotalRows);
Debug.WriteLine("RandNum:" + RandNum + " Low:0 " + "High:" +

TotalRows);

Does anyone know why C# Random function is generating more 1,0,2,3 much

more
frequently than 4,5,6,7,8?

For starters your random seed doesn't look to be very random. Are you
creating/destroying the rndNum object for every number? If so then you
aren't making use of a randomizing algorithm. Every number will be the
first in the sequence based on the supplied seed. If you call it twice
with
the same seed (i.e. within one tick) you will get the same number. You
might notice your numbers repeat often, a sure sign you are within one
tick
on two successive iterations (this would cause a failure on a runs test
for
randomness.) The distribution of random numbers over your range will be
very poor (as you have seen) and a chi-square statistical test won't do
too
well either to test your set of numbers for a large number of iterations.
Try generating the same sequence, but only calling the .Next method inside
your loop and preserve the rndNum object without resetting the seed each
time.
Jack Peacock

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.