Generating random numbers without repeating.C#

前端 未结 11 1878
臣服心动
臣服心动 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 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 listNumbers = new List();
    do
        {
            int numbers = rand.Next(1,49);
            if(!listNumbers.Contains(number)) {
                listNumbers.Add(numbers);
            }
        } while (listNumbers.Count < 6)
    

提交回复
热议问题