mardi 19 juin 2018

Random Numbers without ascending and descending orders [duplicate]

This question already has an answer here:

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;
}




Aucun commentaire:

Enregistrer un commentaire