473,569 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random not really random...

Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.N ow.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 1654
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.R andomNumberGene rator 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.c om> wrote in message
news:QC******** ***********@new s04.bloor.is.ne t.cable.rogers. com...
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.N ow.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.c om> wrote in message
news:QC******** ***********@new s04.bloor.is.ne t.cable.rogers. com...
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.N ow.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.c om> wrote in message
news:QC******** ***********@new s04.bloor.is.ne t.cable.rogers. com...
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.N ow.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*****@simcon v.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
"Maziar Aflatoun" <ma***@rogers.c om> wrote in message
news:QC******** ***********@new s04.bloor.is.ne t.cable.rogers. com...
Hi everyone,

I have the following code in my class method

TheSeed = (int)DateTime.N ow.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
2249
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 each run and then re-counting the list and doing it all over again. My boss thinks that 'b' is somehow less fair than 'a', but the only thing I...
23
4160
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 initialize it with the same parameter. Can this be done with JavaScript? I couldn't find anything in the documentation. Basically, what I want to...
4
8243
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 number for ALL the records. It appears the function is only getting called once, therefore only one random number is being generated for all the...
13
3603
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 repeat the values and that can be called from any class, as I have to call the random number generator from a few different classes. Here is my code:...
15
597
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 generator (actually i just need binary 1 or 0) i would be forever gratefull ;-)
12
5203
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 RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
2
3375
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 steps.. I really want a way to get to the Nth value in a random series started with a particular seed. Is there any way to quickly do what...
20
7824
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 numbers, multiply them, and ask you for the result. It also needs to have four responses for both right and wrong answers and should print them...
41
2320
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() function should be good enough to avoid that, so i may be seeing things. Zytan
24
7192
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 uniformly distributed and unique numbers ?
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.