首页 > 解决方案 > 当我使用 mt19937 在 C++ 中生成随机数时遇到了一个问题

问题描述

我正在尝试使用 c++ 生成随机数mt19937,如代码所示,但我遇到了一些奇怪的行为。

#include <iostream>
#include <random>
#include <chrono>
using namespace std;

int main() {
    mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
    int n = rng() % 10 + 2;
    cout << n << endl;
    for (int i = 0; i < n; i++) {
        int x = rng() % 10 + 1;
        int y = rng() % 10 + 1;
        cout << x << " " << y << " " << endl;
    }
}

当我在“CLion”中运行我的代码时,它会运行并按预期运行;但是,当尝试使用终端运行代码时,它不会编译,我会收到'mt19937' was not declared in this scope 一条错误消息。 这是错误消息屏幕截图

更新 1

#include <bits/stdc++. h>从代码中删除了,但代码仍然不能从终端编译

更新 2

我一直在使用Codeblocks附带的Mingw版本,所以我卸载了它并从SourceForge 重新下载了 Mingw 。令人惊讶的是,问题现在解决了,我可以从终端运行代码。

更新 3

现在,当我使用命令编译一些程序时g++ <name>.cpp,会创建一个名为的文件 a.exe。我不知道为什么它的名字总是a.exe不管原始cpp文件的名字。为了将文件命名.exe为与 cpp 文件相同的名称,我必须编写 g++ <name>.cpp -o <name>. 任何人都可以帮助解释为什么这个名字总是a.cpp

标签: c++random

解决方案


推荐阅读