我想,以填補我的數組在C# 0-9之間唯一的隨機數我試試這個功能:填充在C#中與0-9之間潮頭隨機數的數組
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);
}
}
而且我使用它像這樣的:
for (int i = 0; i < 3; i++)
{
page[i] = UniqueRandom(0, 9);
}
但我得到的錯誤:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'
我還添加了這個名字空間:
using System.Collections.Generic;
我只是不知道我怎麼能轉換函數輸出爲int ...請幫我...謝謝...
您將頁面聲明爲一個int數組,而不是IEnumerable數組... –