Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 14-Jun-10 9:18am
utunity413
Edited 14-Jun-10 11:07am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 | [Rose]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
Smile | :)
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 14 Jun 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100