gpt4 book ai didi

c# - "new Random(x)"总是生成相同的数字?

转载 作者:行者123 更新时间:2023-11-30 13:10:33 24 4
gpt4 key购买 nike

<分区>

我试图获得一个唯一的随机数,但每次运行代码时我总是得到相同的数字。我会先得到 14,然后得到 6,但是我保存所有已用数字的列表似乎不起作用。手动添加 14 可以工作,但是当我添加 randInt 时它不起作用。

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);

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com