Generating random numbers without repeating.C#

前端 未结 11 1803
臣服心动
臣服心动 2020-12-01 17:53

Hi everyone I am trying to generate 6 different numbers on the same line in c# but the problem that i face is some of the numbers are repeating on the same line.Here is my c

相关标签:
11条回答
  • 2020-12-01 17:54

    Create a HashSet and generate a unique random numbers

    public List<int> GetRandomNumber(int from,int to,int numberOfElement)
    {
        var random = new Random();
        HashSet<int> numbers = new HashSet<int>();
        while (numbers.Count < numberOfElement)
        {
            numbers.Add(random.Next(from, to));
        }
        return numbers.ToList();
    }
    
    0 讨论(0)
  • 2020-12-01 17:57

    The best approach (CPU time-wise) for such tasks is creating an array of all possible numbers and taking 6 items from it while removing the item you just took from the array. Example:

    const int min = 1, max = 49;
    List<int> listNumbers = new List<int>();
    int[] numbers = new int[max - min + 1];
    int i, len = max - min + 1, number;
    
    for (i = min; i < max; i++) numbers[i - min] = i;
    for (i = 0; i < 6; i++) {
        number = rand.Next(0, len - 1);
        listNumbers.Add(numbers[number]);
        if (number != (len - 1)) numbers[number] = numbers[len - 1];
        len--;
    }
    
    0 讨论(0)
  • 2020-12-01 18:01

    I've switched your for loop with a do...while loop and set the stopping condition on the list count being smaller then 6. This might not be the best solution but it's the closest to your original code.

    List<int> listNumbers = new List<int>();
    do
        {
            int numbers = rand.Next(1,49);
            if(!listNumbers.Contains(number)) {
                listNumbers.Add(numbers);
            }
        } while (listNumbers.Count < 6)
    
    0 讨论(0)
  • 2020-12-01 18:02

    Instead of using a List, you should use an HashSet. The HashSet<> prohibites multiple identical values. And the Add method returns a bool that indicates if the element was added to the list, Please find the example code below.

    public static IEnumerable<int> GetRandomNumbers(int count)
    {
    HashSet<int> randomNumbers = new HashSet<int>();
    
    for (int i = 0; i < count; i++) 
        while (!randomNumbers.Add(random.Next()));
    
    return randomNumbers;
    }
    
    0 讨论(0)
  • 2020-12-01 18:03

    What you do is to generate a random number each time in the loop. There is a chance of course that the next random number may be the same as the previous one. Just add one check that the current random number is not present in the sequence. You can use a while loop like: while (currentRandom not in listNumbers): generateNewRandomNumber

    0 讨论(0)
  • 2020-12-01 18:05

    Check each number that you generate against the previous numbers:

    List<int> listNumbers = new List<int>();
    int number;
    for (int i = 0; i < 6; i++)
    {
      do {
         number = rand.Next(1, 49);
      } while (listNumbers.Contains(number));
      listNumbers.Add(number);
    }
    

    Another approach is to create a list of possible numbers, and remove numbers that you pick from the list:

    List<int> possible = Enumerable.Range(1, 48).ToList();
    List<int> listNumbers = new List<int>();
    for (int i = 0; i < 6; i++)
    {
      int index = rand.Next(0, possible.Count);
      listNumbers.Add(possible[index]);
      possible.RemoveAt(index);
    }
    
    0 讨论(0)
提交回复
热议问题