2014-03-01 82 views
1

我正在使用C++爲隨機問題創建應用程序。但我不認爲這是有效的(由於我的糟糕的邏輯)。我試圖是這樣的:無法獲得隨機字符串

class English { 
public: 
    string get_questions (int number) { 
     if (number == 1) { 
      // Chapter 1 
      string questions[10] = { 
       "In what way is man considere to be a lower species when compared to animals, in general?", 
       "What specific triats of character make man the lowest animal in Mark Twain's views?", 
       "What aspects of human nature are pointed when man is compared with the anaconda, bees, roosters, cats.", 
       "What specific traits of character make man the lowest animal in Mark Twain's views?", 
       "Discuss the Importance of the experiments conducted by the Mark Twain.", 
       "Can people improve themselves and remove this label in thismillennium?", 
       "What are the traits due to which man cannot claim to have reached the meanest of the Higher Animals?", 
       "\"The damned Human Race\" was written in 1900, is it valid today?", 
       "Do you think Mark Twain was accurate while comparing Human nature to that of the birds, insects and other animals?", 
       "Why did Mark Twain rejected Darwin's theory, what were his conclusions in this regard?" 
      }; 
      string result = questions[rand() % 9 + 0] + "\n"; 
      return result; 
     } 
    } 
}; 

而我使用的代碼是這樣的:

cout << English().get_questions(chapter); 

雖然我有更多的線路,但他們只是簡單的coutcin得到章節和主題值。他們不會因此而麻煩。

這裏的主要問題是,每次我編寫代碼時,編譯和執行代碼時,每次都會提供相同的問題作爲結果。例如,對於目前的隨機邏輯,我得到這個問題:

人們可以在這個千禧年中改善自己,消除這個標籤嗎?

每當我改變邏輯,我會得到一個新的結果,但在每種情況下都是相似的(特定邏輯的代碼執行)!在我想要得到一個隨機問題的地方,每次執行代碼時,我應該改變產生這個隨機數的地方嗎?還是我在其他地方做錯了?

+0

一些評論:C隨機數的設施非常差:「標準」種子是一個時間值('時間(NULL)'),**只有1秒的精度**,並且**不會返回與srand()'expect **相同​​的類型(所以它會失去精度和可變性)。後來,'rand()'不能保證產生均勻分佈(不),但即使在它的情況下,也可以使用模運算符**來打破這種一致性。 – Manu343726

+0

所以(我知道事實並非如此,你所暴露的東西似乎是一個示例練習),如果你的隨機數(你的PRNGs)的cuality是一個問題,考慮**不要使用C庫,搜索質量好的PRNG C++庫**。此外,請注意,自從C++ 11標準庫附帶一個[隨機庫](http://en.cppreference.com/w/cpp/numeric/random),它的等效性比faaaaaaaaaaaaa好。 – Manu343726

回答

5

您應該使用srand函數使用隨機種子值來初始化隨機數生成器,以改變rand()函數的此行爲。 你可以使用類似srand (time(NULL));的東西來初始化使用不同種子的隨機生成器。

請必須在http://www.cplusplus.com/reference/cstdlib/srand/

+1

他應該如何得到「隨機種子值」 - 使用rand()?:) –

+0

@FerdinandBeyer我已經更新了我的答案,詳情和鏈接 –

+0

非常感謝兄弟! :)我從來不知道這種播種的事情,它現在起作用! :) –

2

您還沒有播種的隨機數發生器所以每次運行程序時,你會得到隨機數的同一序列的樣子。在程序開始時使用srand一次。

+0

因此,如果我在程序開始時使用'rand()',或者我創建了另一種方法,那會起作用嗎? –

1

您需要使用標頭<cstdlib>中聲明的函數std::srand來設置隨機序列。

例如

class English { 
public: 
    English() { if (!init) std::srand(unsigned(std::time(0))); init = true; } 

    string get_questions (int number) const { 
     if (number == 1) { 
      // Chapter 1 
      string questions[10] = { /*...*/ }; 
      string result = questions[rand() % 10] + "\n"; 
      return result; 
     } 
    } 
private: 
    static bool init; 
}; 

bool English::init = false; 

要考慮到我如果您使用的是C++ 11兼容的編譯器做在功能get_questions

1

變化,更好的解決方案是使用<random>庫:

// initialize your string array 
std::default_random_engine generator; 
std::uniform_int_distribution<int> distribution(0,9); 
int index = distribution(generator); 
return questions[index];