Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generator with no duplicates

Tags:

c#

Basically I'm creating a program to randomly generate 6 unique lottery numbers so there is no duplicates in the same line, here is the code I have so far...

        //Generate 6 random numbers using the randomiser object

        int randomNumber1 = random.Next(1, 49);
        int randomNumber2 = random.Next(1, 49);
        int randomNumber3 = random.Next(1, 49);
        int randomNumber4 = random.Next(1, 49);
        int randomNumber5 = random.Next(1, 49);
        int randomNumber6 = random.Next(1, 49);

        textBox1.Text = randomNumber1.ToString();
        textBox2.Text = randomNumber2.ToString();
        textBox3.Text = randomNumber3.ToString();
        textBox4.Text = randomNumber4.ToString();
        textBox5.Text = randomNumber5.ToString();
        textBox6.Text = randomNumber6.ToString();

    }

I'm getting random numbers but sometimes there is the same number on the same line, how do I make each number unique????

Thanks in advance

like image 230
Padgey85 Avatar asked Nov 14 '14 13:11

Padgey85


People also ask

Do random number generators repeat?

The numbers generated are not truly random; typically, they form a sequence that repeats periodically, with a period so large that you can ignore it for ordinary purposes. The random number generator works by remembering a seed value which it uses to compute the next random number and also to compute a new seed.


3 Answers

You need to store them in a collection and each time you pick a new number you need to make sure it's not present already, otherwise you need to generate a new number until you find a unique number.

Instead of this, I would generate a sequence between 1 and 49, shuffle them and pick 6 number out of the sequence, for example:

var rnd = new Random();
var randomNumbers = Enumerable.Range(1,49).OrderBy(x => rnd.Next()).Take(6).ToList();
like image 77
Selman Genç Avatar answered Oct 24 '22 00:10

Selman Genç


You can't. You've only specified that each number be a random number from 1 to 49, not that it shouldn't match any duplicates.

Since you've got a relatively small set of numbers, your best bet is probably to draw the random numbers, put them into a HashSet, then if you need more, pull more. Something like this:

HashSet<int> numbers = new HashSet<int>();
while (numbers.Count < 6) {
    numbers.Add(random.Next(1, 49));
}

Here you're taking advantage of the HashSet's elimination of duplicates. This won't work with a List or other collection.

like image 24
James Cronen Avatar answered Oct 24 '22 01:10

James Cronen


Returning repeat values is a necessity in order for a generator to satisfy a necessary statistical property of randomness: the probability of drawing a number is not dependent on the previous numbers drawn.

You could shuffle the integers in the range 1 to 49 and return the first 6 elements. See http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle for more details on such a shuffler.

However, I think you get a slight statistical bias by doing this.

The best way is probably to use random.Next(1, 49); and reject any repeat. That will be free from statistical bias and the fact that you're only wanting 6 from 49 possibilities, the number of collisions will not slow the algorithm appreciably.

like image 30
Bathsheba Avatar answered Oct 24 '22 00:10

Bathsheba