Archived

This topic is now archived and is closed to further replies.

Neo4E656F

C# Random generator class problems?

Recommended Posts

I was just playing around with DirectX9, trying to spawn some objects in front of the screen and I was wondering why when I suddenly put some code in a function that randomly generates the objects position, speed, rotation etc that there was only 1 of 32 objects spawning. Then something strange happened, I put a message box in and it suddenly worked, I took it out again and it didn’t. Instantly it hit me, that there was something wrong with the random number generator. Some further tests confirmed this, creating a new instance of Random within a short time period and calling nextDouble() will give you the same result as the previous instance did just a while ago. Now perhaps I am naïve, but this seems like a major problem/bug? Anyway, here is some source I quickly put together to demonstrate the problem: using System; namespace RandomProblems { class RandomProblemDemo { [STAThread] static void Main(string[] args) { Random random = new Random(); Console.WriteLine("Random c# problem demonstration by Neil Popplewell, neo@koalaclaw.com, 18th of Nov 2003"); Console.WriteLine(); Console.WriteLine("Using one instance of random: "); for(int i=0; i < 8;i++) { WriteRandomData(random); } Console.WriteLine(); Console.WriteLine("Using multiple new instances of random: "); for(int i=0; i < 8;i++) { WriteRandomData(new Random()); } } static void WriteRandomData(Random random) { double i,j,k; i=random.NextDouble(); j=random.NextDouble(); k=random.NextDouble(); Console.WriteLine("i: "+i+" j: "+j+" k: "+k); } } } The output is this: Random c# problem demonstration by Neil Popplewell, neo@koalaclaw.com, 18th of Nov 2003 Using one instance of random: i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 i: 0.906729989176025 j: 0.123466367425149 k: 0.43414923894878 i: 0.0508240740982928 j: 0.775564566150105 k: 0.703305790993993 i: 0.593730231557847 j: 0.817701700058627 k: 0.679160520750638 i: 0.470071501783129 j: 0.81878601844366 k: 0.828636489728762 i: 0.324564047308901 j: 0.241357067246622 k: 0.412099892465444 i: 0.64422380628261 j: 0.188263352582354 k: 0.770307980370851 i: 0.62600353529025 j: 0.842269404252185 k: 0.972056179294389 Using multiple new instances of random: i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 i: 0.566972351431368 j: 0.110285538300073 k: 0.347348872733931 I assume it is not meant to work like this? [EDIT] Ok so it is "meant" to be this way, but I don’t think it should be this way or if there is not already there should be at least be a persistent instance of Random available. [EDIT 2] A nice solution for my problem was this: Random random = new Random(GetHashCode()+unchecked((int)DateTime.Now.Ticks)); as I call this method from within different objects the hashcode is different and hence so are the random values. [edited by - Neo4E656F on November 18, 2003 9:41:58 AM]

Share this post


Link to post
Share on other sites
No, I''d say this behavior is expected. The RNG is, unless you provide a seed yourself, seeded off the system time. If you create two rng objects at roughly the same time, they will generate the same sequence of random numbers.

Instead of creating a new rng object all the time, just keep one around.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

Share this post


Link to post
Share on other sites