我編寫了一個程序,該程序用用戶指定的邊數擲骰子。問題是,這太可以預測了。骰子滾動程序在每次運行時都會產生相同的隨機數序列
我正在使用CodeBlocks IDE,編譯器是GCC。該程序可以很好地編譯爲調試版本和發佈版本,但無論選擇哪種構建選項,可執行文件在每次運行時都會返回相同的值。我不能這樣做,因爲它的預期用途是作爲桌面RPG工具,並且如果智能玩家知道骰子滾動的模式,則相對容易。
解決此問題的最簡單方法是什麼?
這裏的源:
#include <iostream> /* for input and output */
#include <cstdlib> /* for random numbers */
using namespace std;
void rolldie() {
cout << "How many sides to the die?" << endl << "D";
int die;
cin >> die;
int roll = rand() % die +1;
cout << endl << "The die rolled " << roll << endl << endl << "Roll another? (Y for yes, anything else for no; Capitalization counts) ";
}
int main() {
rolldie();
char again;
cin >> again;
while (again == 89) {
rolldie();
cin >> again;
}
return 0;
}
請不要鏈接到你的代碼 - 在這裏表現出來。 – 2012-02-02 18:37:57
使用srand()在調用rand()之前播種隨機數生成器。時間()是一個體面的種子。 – Cameron 2012-02-02 18:37:58
你使用什麼隨機數字發生器?你一定要種下它,但是要告訴你如何*,你必須給我們更多的細節。 – Thilo 2012-02-02 18:39:25