可能的重复:
在 C# 中用 0-9 之间的唯一随机数填充数组 https://stackoverflow.com/questions/10688044/filling-a-array-with-uniqe-random-numbers-between-0-9-in-c-sharp
我有一个像“page[100]”这样的数组,我想用 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);
}
}
这边走 :
int[] page = UniqueRandom(0,9).Take(array size).ToArray();
但它只给了我 9 个唯一的随机数,但我需要更多。
我怎样才能拥有一个包含不相同的随机数的数组?
怎么样
int[] page = new int[100];
Random rnd = new Random();
for (int i = 0; i < page.Length; ++i)
page[i] = rnd.Next(10);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)