好的,所以我有一个掷骰子应用程序...
当我逐步执行代码时,它正常运行并且“结果”包含正确数量的抛出结果并且它们似乎是随机的,当我让代码运行并执行完全相同的事情时,它会产生一组相同的数字。
我确信这是一个我看不到的逻辑错误,但摆弄了几个小时并没有改善这种情况,所以任何帮助都是非常重要的。:)
class Dice
{
public int[] Roll(int _throws, int _sides, int _count)
{
Random rnd = new Random();
int[] results = new int[_throws];
// for each set of dice to throw pass data to calculate method
for (int i = 0; i < _throws; i++)
{
int thisThrow = Calculate(_sides, _count);
//add each throw to a new index of array... repeat for every throw
results[i] = thisThrow;
}
return results;
}
private int Calculate(int _sides, int _count)
{
Random rnd = new Random();
int[] result = new int[_count];
int total = 0;
//for each dice to throw put data into result
for (int i = 0; i < _count; i++)
{
result[i] = rnd.Next(1, _sides);
}
//count the values in result
for (int x = 0; x < _count; x++)
{
total = total + result[x];
}
//return total of all dice to Roll method
return total;
}
}