This question already has an answer here:
- Randomize a List<T> 17 answers
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