0%

如何高效产生m个n范围内的不重复随机数(m<=n)

1
2
3
4
5
6
7
8
9
10
11
var nums = new int[100];
var random = new Random();
for (int i = 0; i < 100; i++)
{
nums[i] = i;
}
for (int i = 0; i < 100; i++)
{
var r = random.Next(i, 99);
Swap(ref nums[i], ref nums[r]);
}