0

I have a problem with a method that generates numbers that do not repeat. Everything goes fine, but I would never want to get a result like: 1,2,3,4 or 4,3,2,1 . The idea is not to receive the numbers in ascending order or in descending order.

Below is the method implemented if someone can help me. Thank you!

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);
    List<int> randomNumbers = new List<int>();
    while (source.Count != 0)
    {
        int randomIndex = UnityEngine.Random.Range(0, source.Count - 1);
        randomNumbers.Add(source[randomIndex]);
        source.RemoveAt(randomIndex);
    }
    return randomNumbers;
}

public static bool IsSorted(List<int> arr)
{
    for(int i=0;i<arr.Count - 1; i++)
    {
        if (arr[i] < arr[i + 1])
            return true;
        else
            return false;
    }
    return true;
}
George
  • 45
  • 7

0 Answers0