【发布时间】:2012-02-02 18:36:13
【问题描述】:
我编写了一个程序,该程序可以掷出用户指定边数的骰子。问题是,它太可预测了。
我使用的是 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;
}
【问题讨论】:
-
请不要链接到您的代码 - 在此处显示。
-
在调用 rand() 之前使用 srand() 播种随机数生成器。 time() 是一个不错的种子。
-
你用的是什么随机数生成器?您当然必须播种,但要告诉您如何,您必须向我们提供更多详细信息。
标签: c++ windows random mingw codeblocks