Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there.. i am currently working on a program that should be generating 3 random numbers and post it on som labels.

the problem is that sometimes it generates 3 numbers where 2 or all 3 of them are equal. How can i avoid that?

Here is my code:


public static void newrand(Label num1, Label num2, Label num3)
       {
           Random rnd = new Random();
           int[] randomnumber = new int[3];
           for (int counter = 0; counter < 3; counter++)
           {
               randomnumber[counter] = rnd.Next(min, max);
           }
           if (randomnumber[0] != randomnumber[1])
           {
               if (randomnumber[0] != randomnumber[2])
               {
                   num1.Text = randomnumber[0].ToString();
               }
               else
               {
                   Random rnd1 = new Random();
                   int i = randomnumber[0];
                   while (i == randomnumber[0] && i == randomnumber[1] && i == randomnumber[2])
                   {
                       i = rnd1.Next(min, max);
                   }
                   num1.Text = randomnumber[0].ToString();
               }


               if (randomnumber[1] != randomnumber[0])
               {
                   if (randomnumber[1] != randomnumber[2])
                   {
                       num2.Text = randomnumber[1].ToString();
                   }
                   else
                   {
                       Random rnd1 = new Random();
                       int i = randomnumber[1];
                      while (i == randomnumber[1] && i == randomnumber[0] && i == randomnumber[2]);

                       {
                           i = rnd1.Next(min, max + 1);
                       }  num2.Text = randomnumber[1].ToString();
                   }
                   if (randomnumber[2] != randomnumber[0])
                   {
                       if (randomnumber[2] != randomnumber[1])
                       {
                           num3.Text = randomnumber[2].ToString();
                       }
                       else
                       {
                           Random rnd1 = new Random();
                           int i = randomnumber[2];
                           while (i == randomnumber[1] && i == randomnumber[0] && i == randomnumber[2])
                           {
                               i = rnd1.Next(min, max);
                           }
                           num3.Text = randomnumber[2].ToString();
                       }

                   }
               }
           }
       }
Posted
Updated 14-Jun-10 10:07am
v2

If you look at what MSDN says about Random() here: http://msdn.microsoft.com/en-us/library/system.random.aspx[^] you will see that it ialks about exactly this case: "that two Random objects that are instantiated in close succession generate an identical series of random numbers."

The solution is to use one Random() object to generate multiple numbers.

See the MSDN page for more info.
 
Share this answer
 
Random numbers generated using System.Random depends on the seed value you pass while creating the object.

So if you are creating 2 random object which you want to act differently you can pass the seed value different and you will see the output.

Other than that, the Default constructor of random calls
GetTickCount API call which returns the number of milliseconds passed after the system starts up.
http://msdn.microsoft.com/en-us/library/ms724408%28VS.85%29.aspx[^]

So if you create two objects in succession, it might be created on same millisecond interval, and hence will give you the same output.

I hope this clears you totally.

:rose:
 
Share this answer
 
As already suggested, don't use multiple instances of Random that way.
To avoid testing for duplicates, have a look at my tip: "Random extraction of 5 cards from a deck" [^](you know duplicated cards would be unpleasant). It is C++, but it is the algo that matters.
:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900