我正在使用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);
雖然我有更多的線路,但他們只是簡單的cout
和cin
得到章節和主題值。他們不會因此而麻煩。
這裏的主要問題是,每次我編寫代碼時,編譯和執行代碼時,每次都會提供相同的問題作爲結果。例如,對於目前的隨機邏輯,我得到這個問題:
人們可以在這個千禧年中改善自己,消除這個標籤嗎?
每當我改變邏輯,我會得到一個新的結果,但在每種情況下都是相似的(特定邏輯的代碼執行)!在我想要得到一個隨機問題的地方,每次執行代碼時,我應該改變產生這個隨機數的地方嗎?還是我在其他地方做錯了?
一些評論:C隨機數的設施非常差:「標準」種子是一個時間值('時間(NULL)'),**只有1秒的精度**,並且**不會返回與srand()'expect **相同的類型(所以它會失去精度和可變性)。後來,'rand()'不能保證產生均勻分佈(不),但即使在它的情況下,也可以使用模運算符**來打破這種一致性。 – Manu343726
所以(我知道事實並非如此,你所暴露的東西似乎是一個示例練習),如果你的隨機數(你的PRNGs)的cuality是一個問題,考慮**不要使用C庫,搜索質量好的PRNG C++庫**。此外,請注意,自從C++ 11標準庫附帶一個[隨機庫](http://en.cppreference.com/w/cpp/numeric/random),它的等效性比faaaaaaaaaaaaa好。 – Manu343726