Avoid random duplicates

System.Random generator = new Random(DateTime.Now.Millisecond); int[] lotteryNumber = new int[7]; Console.WriteLine("Your lottery numbers: "); for (int i = 0; i<7; i++) { lotteryNumber[i] = generator.Next(1, 37); Console.Write("{0} ",lotteryNumber[i]); } Console.ReadLine(); 

I need to create a program that prints 7 lottery numbers, but without duplicates. The above code outputs 7 random numbers in the range (1-37), but duplicates the appar. I need a way to prevent duplicate numbers from appearing.

+4
source share
9 answers

The simplest IMO approach would be to generate a sequence of all possible numbers (i.e. 1-37), shuffle the collection, and then accept the first seven results.

A stack overflow search for "Fisher-Yates shuffle C #" will find many examples.

In fact, you can modify Shuffle Fisher-Yates to get results as they are taken, so you can write a method such as:

 var numbers = Enumerable.Range(1, 37).Shuffle().Take(7).ToList(); 
+12
source

You can take the dictionary, but make sure you prevent the key from being reinserted. The dictionary keys will serve as the unique numbers you need.

+1
source

You can drop them into the HashSet<int> . If you are Add and it returns false, generate a new number.

0
source

If you are trying to select numbers from a range without repetition, you need to create an array of all possible numbers, and then "shuffle" the random selection:

 int[] allPossibleNumbers = Enumerable.Range(1, 37).ToArray(); int[] lotteryNumber = new int[7]; for (int i = 0; i < 7; i++) { int index = r.Next(i, 37); lotteryNumber[i] = allPossibleNumbers[index]; allPossibleNumbers[index] = allPossibleNumbers[i]; // This step not necessary, but allows you to reuse allPossibleNumbers // rather than generating a fresh one every time. // allPossibleNumbers[i] = lotteryNumber[i]; } 
0
source

Create a list with 37 items. Then in your application select one and delete the selected

0
source

Perhaps this may help if you get an existing number, just try to find a new one that is not in the array:

 static void Main(string[] args) { System.Random generator = new Random(DateTime.Now.Millisecond); int[] lotteryNumber = new int[7]; Console.WriteLine("Your lottery numbers: "); for (int i = 0; i < 7; i++) { int lNumber = 0; do { lNumber = generator.Next(1, 37); } while (lotteryNumber.Contains(lNumber)); lotteryNumber[i] = lNumber; Console.Write("{0} ", lotteryNumber[i]); } Console.ReadLine(); } 
0
source
 HashSet<int> set = new HashSet<int>(); System.Random generator = new Random(DateTime.Now.Millisecond); while(set.Count < 7){ set.Add(generator.Next(1,37); } 

This should work as the HashSet automatically ignores duplicates. Just loop until the set reaches the required number of units. The only potential problem is that the POTENTIAL (unlikely) cycle lasts a long time, but in the end it must respond.

0
source

so I took your source code ... found some logical errors and added the fix you were looking for to prevent duplicate random numbers.

Enjoy it!

  System.Random generator = new Random(DateTime.Now.Millisecond); int[] lotteryNumber = new int[7]; int lowerBounds = 1; int upperBounds = 8; int maxNumberLotteryValues = 7; if ( ( upperBounds - lowerBounds ) < (maxNumberLotteryValues)) { Console.Write("Warning: Adjust your upper and lower bounds...there are not enough values to create a unique set of Lottery numbers! "); } else { Console.WriteLine("Your lottery numbers: "); for (int i = 0; i < maxNumberLotteryValues; i++) { int nextNumber = generator.Next(lowerBounds, upperBounds); int count = lowerBounds; //Prevent infinite loop while ((lotteryNumber.Contains(nextNumber)) && (count <= upperBounds)) { nextNumber = generator.Next(lowerBounds, upperBounds); count++; //Prevent infinite loop } lotteryNumber[i] = nextNumber; Console.Write("{0} ", lotteryNumber[i]); } } Console.ReadLine(); 
0
source
 const int nBalls = 37; const int nPicks = 6; int[] balls = new int[nPicks]; Random rnd = new Random(DateTime.Now.Millisecond); int remainingBalls=nBalls; int remainingPicks=nPicks; for (int i = 1; i <= nBalls; i++) { if (rnd.Next(1, remainingBalls+1) <= remainingPicks) balls[--remainingPicks]=i; remainingBalls--; } Console.WriteLine(string.Join(",",balls)); 

Will outperform Shuffle and HashSet methods as nPicks / nBalls gets bigger.

0
source

Source: https://habr.com/ru/post/1443653/


All Articles