我有两个List<int>
,我们打电话给一个Pool
,另一个Used
。乱序无法输出两次相同的号码的列表
Pool
有0
和X
和Used
具有已从Pool
名单中抽调整数之间的整数。
所以我想要实现的是从Pool
列表中获得随机抽签,其中抽签不包含在Used
列表中的任何地方。如果我已经使用了所有数字,我希望它跳回0
。
这就是我想出了,但它没有工作
int x = 0;
int ri = 0;
Random r = new Random();
while (true)
{
ri = r.Next(0, Pool.Count);
if (!Used.Contains(ri))
{
break;
}
else
{
x += 1;
}
if(x == Used.Count) // Here I check if it has tried getting a new random number but failed since the Pool is "empty"
{
ri = 0;
UsedIndexes = new List<int>();
break;
}
}
当我用这个,但它确实在那里从0-2-4-6-8
等等,我不知道为什么跳到一些疯狂的事情。
不应该这样工作吗?我如何解决它?
http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle –
发布的代码是否编译? – NoChance
你怎么“得到”什么?这段代码不会输出任何内容。 –