2015-07-11 48 views
-3

***問題已得到解答,問題的癥結在於 「使用C++編程原則和實踐」一書中的錯誤,其中srand可接受與頭文件中定義的函數randint()一起使用。爲什麼不是srand種子改變結果?

我希望答案不太明顯,但我環顧四周,似乎無法弄清楚爲什麼srand不會改變bull_pen向量中的值。每次運行我都會得到相同的4個整數。

我已經讀過關於使用計算機時鐘的所有信息,確保只在循環等或包含stdlib.h之外使用srand,但這些都不會有幫助。 使用用戶輸入作爲種子看起來很容易,但現在我覺得我在某處丟失了一些基本理解 。 如果我有不好的風格等,我會提前道歉 - 對C++和一般編程來說都很新穎。謝謝您的幫助。

#include "..\..\std_lib_facilities.h" 

int main() 
{ 
    int seed; 
    cout << "Before the game begins, enter any integer.\nThis will generate a seed to randomize the game: "; 
    cin >> seed; 
    srand(seed); 
    cout << "I have four numbers in a sequence for you to guess." << endl 
     << "For every digit you guess that matches a digit in my sequence, I will tell you\nthat you guessed that many 'cows'." << endl 
     << "For ever digit you guess that matches a digit AS WELL as matches the digits\nlocation in my sequence, I will say you guessed" << endl 
     << "that many 'bulls'. Guessing all four 'bulls' wins the game.\n"; 

    while (1 == 1){ 

     vector<int>bull_pen(4); 
     bull_pen[0] = randint(9); 
     bull_pen[1] = randint(9); 
     bull_pen[2] = randint(9); 
     bull_pen[3] = randint(9); 

     vector<int>guesses(4); 
     int guess; 


     int found = 0; 
     int bulls = 0; 
     int cows = 0; 

     while (found != 1){ 
      vector<int>bull_pen_flags(4); 

      cout << "Please enter your guesses: "; 
      for (int i = 0; i < guesses.size(); i++){ 
       cin >> guess; 
       guesses[i] = guess; 
      } 

      for (int i = 0; i < bull_pen.size(); i++){ 
       if (bull_pen[i] == guesses[i]) { bulls++; bull_pen_flags[i] = 1; } 
      } 

      if (bulls < 4){ 
       for (int guess_index = 0; guess_index < guesses.size(); guess_index++){ 
        for (int bull_index = 0; bull_index < bull_pen.size(); bull_index++){ 
         if (guess_index != bull_index && bull_pen_flags[bull_index] != 1){ 
          if (guesses[guess_index] == bull_pen[bull_index]) { 
          cows++; 
          bull_pen_flags[bull_index] = 1; 
         } 
         } 
        } 
       } 


       if (cows > 0) cout << cows << " cow(s).\n"; 
       else cout << "No cows.\n"; 
       if (bulls > 0) cout << bulls << " bull(s).\n"; 
       else cout << "No bulls.\n"; 
       bulls = 0; 
       cows = 0; 
      } 

      else found = 1; 

     } 
     cout << "You've guessed all four bulls; " << bull_pen[0] << bull_pen[1] << bull_pen[2] << bull_pen[3] << "\nYou win!\n"; 
    } 
} 
+4

什麼是'randint'?它是如何定義的? –

+0

不知道。這可能來自我使用的頭文件,我從一本書中學習了C++。有更普遍的選擇嗎?我見過使用rand,所以我有一種感覺,用rand替代randint可以提供幫助嗎? –

+1

如果你輸入相同的'seed',那麼'rand()'每次都會產生相同的序列。這就是rand()的定義。通常情況下,你可以使用srand(time(0));'爲了讓它每次運行它,以便它產生不同的序列。 –

回答

0

randint不是標準庫函數。它是您使用的標題中的一項功能,它不是標準庫的一部分。它只是Bjarne Stroustrup的「使用C++編程原則和實踐」的入門庫的一部分。源文件是here,和該函數的定義如下:

inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); } 

inline int randint(int max) { return randint(0, max); } 

正如你可以看到,它使用default_random_engine隨機數生成,這是C++ 11標準庫的一部分。 srand對此引擎沒有影響。有種種子的方法,但是Bjarne並沒有通過他提供的接口來暴露它(我想他只是試圖在這個庫中儘量簡單,因爲它只是用於介紹目的)。

如果你想要一個隨機數發生器,你可以種子,那麼你可以繼續使用srand種子,但使用rand()產生你的號碼。更好的是,您可以使用與randint函數使用的引擎相同的引擎。

std::default_random_engine engine(seed); 
std::uniform_int_distribution<> dist(0, 9); 

int x = dist(engine); 
+0

嗯...這本書說:*您可以通過從std_lib_facilities.h中調用隨機數生成器randint(10)四次隨機數字四次。您會注意到,如果您重複運行該程序,它會在每次啓動程序時選取相同的四位數序列。爲了避免這種情況,要求用戶輸入一個數字(任何數字)並調用srand(n),其中n是用戶在調用randint(10)之前輸入的數字。這種n被稱爲種子,不同的種子會產生不同的隨機數序列。* –

+0

@BlueMoon:您必須使用舊版本,它使用'rand'。如果新版本仍然這樣說,那麼這是一個錯誤。 –

+0

是的,我有舊的。我會驗證你說什麼時,我得到的新版本:D –

0
inline int randint(int min, int max,int seed) { static default_random_engine ran(seed); 
    return uniform_int_distribution<>{min, max}(ran); } 

inline int randint(int max,int seed) { return randint(0, max,seed); } 

您可以修改頭文件,就像上面,通過種子的默認引擎。這裏是我的程序的版本

#include "std_lib_facilities.h" 

int main(){ 
    cout << "Enter any number to start the game"; 
    int seed; 
    cin >> seed; 
    vector<int> v(4); 

    // For generating randon numbers 

    for (int i = 0; i < v.size(); i++) 
     v[i] = randint(9,seed); 

    // For generating random four different digits 

    for (int i = 1 ; i < v.size(); i++){ 
     for (int j = i; j < v.size(); j++){ 
      while (v[i-1] == v [j]) 
       v[i-1] = randint(9,seed); 
     } 
    } 
    int bull = 0, cow = 0, tries = 0; 
    cout << "I have a four digit number can you guess it??\n"; 
    cout << "lets start the game!!!\n "; 
    int number; 
    while (bull != 4){ 
     tries++; 
     bull = 0; 
     cow = 0; 
     cout << "Guess the number: "; 
     cin >> number; 
     if (number > 1000 && number < 9999){ 

     for (int i = 0; i < v.size(); i++){ 
       if (v[i] == number % 10){ // to see digit is correct 
        if (i == v.size()-1)// to see 1's position is correct 
         bull++; 
        else cow++; 
       } 
       if (v[i] == (number % 100)/10){ // to see digit is correct 
        if (i == v.size()-2)// to see 10's position is correct 
         bull++; 
        else cow++; 
       } 
       if (v[i] == (number/100) % 10){ // to see digit is correct 
        if (i == v.size()-3)// to see 100's position is correct 
         bull++; 
        else cow++; 
       } 
       if (v[i] == number/1000){ // to see digit is correct 
        if (i == v.size()-4)// to see 1000's position is correct 
         bull++; 
        else cow++; 
       } 

     } 

     cout << "Digits correct = " << bull+cow << "\n" << "Position correct = " << bull << "\n"; 
     } 
     else 
      cout << "Enter only four digit numbers\n"; 
    } 
    cout << "CONGRATS!!!..YOU GUESSED THE NUMBER IN " << tries << " tries\n"; 
    return 0; 
} 

隨着值,傳遞種子默認引擎運行初始化。我希望它能解決問題