嗨
这是我如何使用随机但它总是给indexOfAChosenListCell"1"索引.
当我调试它时会显示不同的值,但是在常规运行时我每次都会得到相同的移动..
Random有什么问题,它是静态的不是随机的...... :)
internal Square getAutomaticMove()
{
List<Square> LegalMovesArray = GetLegalSquares();
Random randomListCell = new Random();
int indexOfAChosenListCell = 0;
if (CheckForLegalSquares())
{
indexOfAChosenListCell = randomListCell.Next(LegalMovesArray.Count-1);
}
Run Code Online (Sandbox Code Playgroud)
小智 4
我建议也为随机数生成器播种。据我了解,每次运行应用程序时,您最终都会得到相同的伪随机数序列。我可能错了。
int Seed = (int)DateTime.Now.Ticks;
Random randomListCell = new Random(Seed);
Run Code Online (Sandbox Code Playgroud)