骰子滚动程序在每次运行时生成相同的随机数序列

use*_*827 2 c++ windows random mingw codeblocks

我写了一个程序,用一个用户指定的边数滚动模具.问题是,它太可预测了.

我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)

Car*_*rum 8

您没有为随机数生成器播种.我不会下载你的zip文件,但是因为你正在使用MinGW,我想你可能想要查看srandom(3)或者srand(3).