好吧,我有一個骰子扔應用程序...C#代碼只在步驟中給出預期的結果?
當我通過代碼它的功能正常和'結果'包含正確數量的投擲結果,他們似乎是隨機的,當我離開代碼運行並做完全相同的事情,它會產生一組相同的數字。
我確定這是一個邏輯錯誤,我看不到,但幾個小時擺弄它沒有改善情況,所以任何幫助都非常appriciated。 :)
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;
}
}
感謝修改:) – Yoda 2009-09-29 13:35:35
該死的快速繪製! – 2009-09-29 13:36:40
@Ian:我已經看到這發生了很多次,不能立即發現:) – leppie 2009-09-29 13:37:39