473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Random not really random...

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
4 1636
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bart Nessux | last post by:
I am using method 'a' below to pick 25 names from a pool of 225. A co-worker is using method 'b' by running it 25 times and throwing out the winning name (names are associated with numbers) after...
23
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
4
by: Greg Strong | last post by:
Hello All, Is it possible to create multiple random numbers in a query where there are numerous records? I've created a custom function. When I use it in a query it creates the same random...
13
by: Roy Gourgi | last post by:
Hi, How do I invoke the random number generator that was suggested by a few people. Ideally, what I would like to do is to instantiate the random no. generator with a seed value that does not...
15
by: felixnielsen | last post by:
This is something i have done before and i know its pretty simple, however i cant remember how it works exactly, and i need it i kinda hurry, so if someone would be so nice to drop a random number...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
2
by: Matthew Wilson | last post by:
The random.jumpahead documentation says this: Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many...
20
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random...
41
by: Zytan | last post by:
Anyone do any tests on it? I would assume it has improved since C's rand(), but who knows. For some reason, and it could just be coincidence, I seem to see patterns. But even a crappy rand()...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.