我想获得一个唯一的随机数,但每次运行代码时我都会得到相同的数字。我会先获得14,然后是6,但我的列表中保存所有使用的数字似乎并没有工作。加入14手动工作,但是当我添加randInt它不起作用。“新的随机(x)”总是生成相同的数字?
const int numCards = 32;
List<int> usedCards = new List<int>();
int randInt = 0;
Random rand = new Random(numCards);
usedCards.Add(14);
do
{
randInt = rand.Next(0, numCards);
MessageBox.Show(randInt.ToString(), "End Game", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
while (usedCards.Contains(randInt));
usedCards.Add(randInt);
@Alexei:这是一个不同的错误 - 这是在每次使用作出新的'Random'。 – sarnold 2012-04-20 02:19:48
@AlexeiLevenkov“几乎”,但这是更具体...。不过,一个好的相关问题。 – 2012-04-20 02:20:02