Random not really random...

  • Thread starter Maziar Aflatoun
  • Start date
M

Maziar Aflatoun

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.
 
K

Kevin Spencer

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.
 
J

Jack Peacock

Maziar Aflatoun said:
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
 
M

Maziar Aflatoun

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top