所以我试图做一个随机提示发生器,显然,不应该表现出同样的尖端至少要等到所有的人都被之前获取不是最后一个的随机数?
private string getRandomTip()
{
List<string> Titles = new List<string> {
"You can copy the result by clicking over it",
"Remember to press Ctrl + Z if you messed up",
"Check web.com to update the app"};
Random rnd = new Random();
int Index = rnd.Next(0, Titles.Count);
string randomString = Titles[Index];
Titles.RemoveAt(Index);
return randomString;
}
看到,但由于某种原因,这重复两次提示或更多。我认为我可以生成随机数,直到我得到一个不是最后一个的数字,但这看起来像是一个糟糕的优化代码。
你能帮我吗?
编辑:好的家伙非常感谢你,我这个想现在
public List<string> Titles = new List<string> {
"You can copy the result by clicking over it",
"This calculator was created by Glow on 21/10/17",
"Click on this tip to show the next one"};
private string getRandomTip()
{
string randomString;
if (Titles.Count > 0) {
Random rnd = new Random();
int Index = rnd.Next(0, Titles.Count);
randomString = Titles[Index];
Titles.RemoveAt(Index);
}
else
{
randomString = "Those were all the tips!";
}
return randomString;
}
和它完美的作品。再次感谢:)
每次方法被调用列表重新初始化。从这个临时列表中删除不会影响下次通话发生的任何事情。它会被所有的人重新填满。您需要将该列表存储在方法之外。 –
建议您[阅读文档](https://msdn.microsoft.com/en-us/library/system.random(v = vs.110).aspx)。用默认的构造函数(相同的种子)创建一个新的随机数将创建相同的随机数序列。第1节第3段将使您朝正确的方向发展。 –
这就对了非常感谢你,我已经更新了它,看起来像现在的作品。谢谢:) – Glow