[Solved] Unordered Random Numbers [closed]


if you want to generate random numbers in range without repeating, you can try this:

public static List<int> GenerateNumbers(int mn,int mx)
{
    List<int> source = new List<int>();
    for (int i = mn; i <= mx; i++)
        source.Add(i);
    var random = new Random();
    List<int> randomNumbers = new List<int>();
    while(source.Count != 0)
    {
        int randomIndex = random.Next(source.Count);
        randomNumbers.Add(source[randomIndex]);
        source.RemoveAt(randomIndex);
    }
    return randomNumbers;
}

usage example:

foreach (int x in GenerateRandomNumbers(2, 5))
        Console.WriteLine(x);

also you can seed it with your own list

1

solved Unordered Random Numbers [closed]