I want to fill my array with unique random numbers between 0-9 in c#
I try this function:
IEnumerable<int> UniqueRandom(int minInclusive, int maxInclusive)
{
List<int> candidates = new List<int>();
for (int i = minInclusive; i <= maxInclusive; i++)
{
candidates.Add(i);
}
Random rnd = new Random();
while (candidates.Count > 1)
{
int index = rnd.Next(candidates.Count);
yield return candidates[index];
candidates.RemoveAt(index);
}
}
And I use it like this:
for (int i = 0; i < 3; i++)
{
page[i] = UniqueRandom(0, 9);
}
But I got error :
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'
I also added this name space:
using System.Collections.Generic;
I just don't know how I can convert the function output to int... please help me... thank you...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…