2014-12-06 124 views
0

這裏是我所創建的功能:我想它選擇0或1隨機隨機選擇0和1之間

tJugador quienEmpieza(){ 
    rand() 
    tJugador whoStarts = tJugador(rand() % 2); // randomly assigns player 
    return whoStarts; 

。它每次選擇0。應該隨機選擇玩家角色或玩家自動機的遊戲。我只需要搞清楚如何使功能選擇0或1

這裏是整個方案與建議的更改幫助:

#include <iostream> 
#include<ctime> 
#include<cstdlib> 
#include <string> 

using namespace std; 

const int Meta = 31; 
enum tJugador { Automata, Persona }; 


tJugador quienEmpieza(); 
tJugador pasalacalculador(); 

int digitoAleatorio(); 
int digitoAutomata(int ultimo); 

int digitoPersona(); 
int digitoPersona(int ultimo); 

bool mismaFila(int ultimo, int nuevo); 
bool mismaColumna(int ultimo, int nuevo); 
bool digitoValido(int ultimo, int nuevo); 



int main() 

{ 
    std::srand(time(NULL)); 

    int pasalacalculadora(); 
    int whoStarts = 0; 
    int d = 0; 
    int suma; 
    suma = 0; 
    string nom; 
    int ultimo; 
    ultimo = 0; 
    int x = 0; 
    int next = 0; 

    cout << "Bienvenido a Pasa La Calculadora!" << endl; 
    cout << "Como Te llamas?"; 
    cin >> nom; 
    cout << "Hola:" << nom << endl; 
    cout << "Empiezas:"; 

    if (whoStarts == 0) 
    { 
     std::cout << "Automata" << endl; 


    } 
    else if (whoStarts == 1) 
    { 
     std::cout << "Persona" << endl; 

    } 


    while ((x != 0) && (suma < Meta)) { 

     cout << "Introduce un numero (0 para abandonar): "; 
     cin >> d; 

     cout << "Suma =" << suma << endl; 

     if (whoStarts == Automata){ 

      d = digitoAutomata(ultimo); 
      suma = suma + d; 
      ultimo = d; 
      next = Persona; 
     } 

     if (whoStarts == Persona){ 

      x = digitoPersona(); 
      suma = suma + x; 
      ultimo = x; 
      next = Automata; 
     } 


    } 
    cin.sync(); 
     cin.get(); 
     system("pause"); 
     return next; 

} 



tJugador quienEmpieza(){ 
    rand(); 
    tJugador whoStarts = tJugador(rand() % 2); // randomly assigns player 
    return whoStarts; 
} 




int digitoAleatorio(){ 
    return (rand() % 9) + 1;         // randomly assigns a number 


    if (quienEmpieza == 0) 
    { 
     std::cout << "Introduce un digito:" << digitoAleatorio << endl; 
     std::cout << ("Suma:") << digitoAleatorio << endl; 

    } 


} 

int digitoAutomata(int ultimo){ 
    bool validDigit = false; 
    int digitoAleatorio = 0; 
    int d = 0; 

    if (digitoAleatorio == validDigit) { 

     (d = true); 



     return d; 
    } 
} 


int digitoPersona(){ 
    int x; 
    int whoStarts = 0; 

    if (whoStarts == 1) 
    { 
     cout << "Introduce un numero entre 0 y 9(0 para abandonar): "; 
     cin >> x; 
    } 

    if ((x >= 1) && (x <= 9)) 

    { 
     cout << ("Suma:") << x << endl; 
    } 
    return x; 
}  
} 
+0

tJugador究竟做了什麼?發佈代碼。順便說一下,whoStarts的類型是'tJugador'而不是'int' – nbro 2014-12-06 16:39:18

+0

它選擇了人物角色或自動機 – Create881 2014-12-06 17:08:58

+1

我看不到你在哪裏調用函數'quienEmpienza'。在你的函數中,'digitoAleatorio'你有一個if語句來檢查'quienEmpieza == 0' ,但是這個全局變量在哪裏?也許你想調用這個函數?所以它會是'quienEmpieza()== 0'? – nbro 2014-12-06 17:19:33

回答

2

我看到你的方法有幾個問題:

  • 你使用%3應從{0,1,2}
  • 你初始化每次調用隨機時間的種子選擇一個隨機數

通常,您應該只初始化您的隨機數發生器一次,然後多次調用rand()。所以我的問題是:您撥打rand()多少次來評估生成的值的分佈?

隨機數字的事情是,它們是隨機的,因此調用rand()兩次或三次將產生三個連續的0值是有可能的。最重要的是,rand()是一個僞隨機數發生器,這意味着它不是隨機的,它只是設計看起來隨機。

要評估結果分佈,您應該生成幾千個數字(甚至數十或數十萬),然後查看0的數目是否大約等於您獲得的1的數量。

+0

進行了您所建議的更改,但每次仍然返回0 ... – Create881 2014-12-06 17:13:25

-3

在程序開始時使用srand 一次例如,

int main() 
{ 
    srand(time(nullptr)); 
    // do work 
} 

我會從更大的頻譜,例如, 1000並檢查數字是奇數還是偶數,並因此確定0或1,例如 ,例如

num = rand() % 1000; 
if (num % 2 == 0) 
    // 0 
else 
    // 1 
// or the other way around 
//or simply go 
rand() % 2; // was % 1 
+2

'rand()%1'將始終爲零。 – slugonamission 2014-12-06 16:41:19

+0

哦,大家好,所有的語言和非標準我混在一起,它是一個開放的Intervall,所以很抱歉...將正確 – smoothware 2014-12-06 16:42:57

1

我沒有在您的代碼中看到您正在調用函數quienEmpienza。在你的函數digitoAleatorio你有一個if語句檢查quienEmpieza == 0,但這個全局變量是quienEmpieza?也許你想直接在if語句中調用函數?那麼它會是quienEmpieza() == 0

我想你總是0,因爲whoStarts被初始化爲0,並且你不會在任何地方調用quienEmpieza,正如我上面提到的。

+1

表達式'quienEmpienzia == 0'將檢查指向函數'quienEmienzia'是空指針。當然,它從來不是空指針,所以表達式總是被評估爲「假」。 – celtschk 2014-12-07 20:54:55