November 7, 2004

Imprinting on "randomness"

Well, that's just a simple level 100 quiz aiming to imprint "standard random number generators are not really random" program to those who still lack it. What will produce the following C# snippet?

System.Random rnd = new System.Random(12345);
System.Random rnd2 = new System.Random(12345);
for (int i=0; i<1000; i++)            
if (rnd.Next() != rnd2.Next())
    Console.WriteLine("Truly random!");

November 7, 2004 2:05 PM | #.NET
Comments

A more subtle variation occurs if you create an instance of a System.Random on each of a pool of threads at startup. If you're not careful they can all start with the same seed (based on the clock) and produce identical sequences. Not usually what you mean to do ;)

Posted by: Jason Whittington at November 20, 2004 8:58 PM

The result will be the empty string.

It is wellknown that if you start from the same seed you'll get the same sequence of numbers.


Cheers,
Dimitre.

Posted by: Dimitre at November 7, 2004 9:31 PM
Post a comment




Remember Me?

Trackback Pings

Listed below are links to weblogs that reference this post:

IndexingXPathNavigator reloaded from eXtensible mind
Tracked on November 14, 2004 4:30 AM