2013-02-16 55 views
1

這寫的是模擬拋出2個6面骰子。但是當我投入10次時,它會隨機拋出儘可能多的數(4,5,6等)。我錯過了什麼嗎?骰子計數器不能正常工作(初學者)

#include <iostream> 
#include <cmath> 
#include <cstdlib> 
#include <ctime> 

using namespace std; 

int throwDice()    // returns random number ranged 2-12 
{ 
    int x = (rand() % 11) + 2; 
    return x; 
} 


int main() 
{ 
    srand (time(NULL)); 
    int y; 
    cout << "Roll dice how many times?" << endl; 
    cin >> y; 

    int a2 = 0; 
    int a3 = 0; 
    int a4 = 0; 
    int a5 = 0; 
    int a6 = 0; 
    int a7 = 0; 
    int a8 = 0; 
    int a9 = 0; 
    int a10 = 0; 
    int a11 = 0; 
    int a12 = 0; 

    for (int i = 0; i < y; i++) 
    { 
     throwDice(); 

    if (throwDice() == 2) 
     a2++; 
    else if (throwDice() == 3) 
     a3++; 
    else if (throwDice() == 4) 
     a4++; 
    else if (throwDice() == 5) 
     a5++; 
    else if (throwDice() == 6) 
     a6++; 
    else if (throwDice() == 7) 
     a7++; 
    else if (throwDice() == 8) 
     a8++; 
    else if (throwDice() == 9) 
     a9++; 
    else if (throwDice() == 10) 
     a10++; 
    else if (throwDice() == 11) 
     a11++; 
    else if (throwDice() == 12) 
     a12++; 
    } 
    cout << "2 = " << a2 << endl; 
    cout << "3 = " << a3 << endl; 
    cout << "4 = " << a4 << endl; 
    cout << "5 = " << a5 << endl; 
    cout << "6 = " << a6 << endl; 
    cout << "7 = " << a7 << endl; 
    cout << "8 = " << a8 << endl; 
    cout << "9 = " << a9 << endl; 
    cout << "10 = " << a10 << endl; 
    cout << "11 = " << a11 << endl; 
    cout << "12 = " << a12 << endl; 

    system("pause"); 
} 

回答

6
  • 要調用throwDice()一旦生成扔在每一個分析的狀態(正確的),然後再在你的if語句(不正確)。將throw的結果保存在一個變量中,並在您的支票中使用該變量。

  • throwDice函數執行模擬被投擲兩個6面的骰子,但它模擬一個11面的骰子(印有號碼2-11)被拋出。這是有區別的。 (這不是一個規劃問題,雖然,但數學的。一旦你瞭解它背後的數學,可以很容易地糾正你的功能。)

1

你的循環應該像

int result; 
for (int i = 0; i < y; i++) 
{ 
    result = throwDice(); 

if (result == 2) 
    a2++; 
else if (result == 3) 
    a3++; 
else if (result == 4) 
    a4++; 
else if (result == 5) 
    a5++; 
else if (result == 6) 
    a6++; 
else if (result == 7) 
    a7++; 
else if (result == 8) 
    a8++; 
else if (result == 9) 
    a9++; 
else if (result == 10) 
    a10++; 
else if (result == 11) 
    a11++; 
else if (result == 12) 
    a12++; 
} 

而且,throwDice()函數不等於投擲2個骰子。你已經創建了一個函數,將所有值從2滾動到12的機會相同。當擲出2個骰子時,例如擲出6的概率比擲出12的概率大得多。你應該創建兩個數字1和6並添加它們以獲取throwDice()函數的返回值。

+0

這似乎別人打我在我輸入的時候這兩個點都是這樣。 – Alex 2013-02-16 08:55:29

2

我會寫這個代碼,而不是:

#include <iostream> 
#include <cmath> 
#include <cstdlib> 
#include <ctime> 

using namespace std; 

int throwDice()    // returns random number ranged 2-12 
{ 
    int x = (rand() % 11) + 2; 
    return x; 
} 

int main() 
{ 
    srand (time(NULL)); 
    int y; 
    cout << "Roll dice how many times?" << endl; 
    cin >> y; 
    int total[13]; 
    for(int i = 2; i <= 12; i++) 
     total[i] = 0; 

    for (int i = 0; i < y; i++) 
    { 
     total[throwDice()]++; 
    } 

    for (int i = 2; i <= 12; i++) 
     cout << i << " = " << total[i] << endl; 

    system("pause"); 
} 

更簡單,更容易理解。

嗯,這裏是你的代碼的問題:

您反覆調用throwDice函數中的所有語句if。您只需在循環的每次迭代中調用一次,存儲結果並比較結果。你不應該每次都進行比較。每個電話都會給你一個新的結果。

你也可以在我的代碼中看到如何使用數組簡化整個代碼。在這裏,我浪費了兩個數組元素(0和1),這可以通過索引簡單的算術來避免。

+0

不需要循環來初始化'total':'int total [13] = {};' – billz 2013-02-16 08:58:42

+0

@billz哦,你是對的。我很少用C++編寫代碼。 – 2013-02-16 08:59:58

+1

不同意你的最後一句話:那幾個字節爲了一個非常高尚的目的而犧牲,當然不會被浪費。如果你在這個特定的例子中做了索引欺騙,那麼你會浪費代碼的清晰度,並且更容易引入錯誤的錯誤。 – us2012 2013-02-16 09:00:07

0

我知道這並不能回答這個問題,但我忍不住想知道你是否可能受益於C++ 11,如果它提供給你:

#include <random> 

std::minstd_rand prng; 
std::uniform_int_distribution<int> dice (1, 6); 

int throwDice() 
{ 
    return (dice(prng) + dice(prng)); 
} 

int main() 
{ 
    prng.seed(time(NULL)); 

    // (code) 
}