Avoiding random duplicates

后端 未结 9 1043
情深已故
情深已故 2020-12-03 19:52
System.Random generator = new Random(DateTime.Now.Millisecond);
int[] lotteryNumber = new int[7];

Console.WriteLine(\"Your lottery numbers: \");
for (int i = 0; i&l         


        
相关标签:
9条回答
  • 2020-12-03 20:08

    If you're trying to pick numbers from a range without repetitions, you need to create an array of all the possible numbers and then "shuffle" a random selection out:

    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 讨论(0)
  • 2020-12-03 20:17

    You could take a dictionary but make sure that you prevent duplicate key insertion. Keys of dictionary would serve as the unique numbers you need

    0 讨论(0)
  • 2020-12-03 20:20

    You could toss them into a HashSet<int>. If you Add and it returns false, generate a new number.

    0 讨论(0)
  • 2020-12-03 20:24

    Generate a list with your 37 items. Then in your for, select one and delete the selected

    0 讨论(0)
  • 2020-12-03 20:25

    so I took your original code...found some logic errors and added the fix you were looking for to prevent random number duplicates.

    Enjoy!

     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 讨论(0)
  • 2020-12-03 20:27

    Maybe this could help, if you get the existing number just try to find new one that isn't 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 讨论(0)
提交回复
热议问题