LoginSignup
0

More than 5 years have passed since last update.

C++ MinGWを使用した場合にstd::random_deviceが毎回同じ値を出力する問題について

Posted at

表題の通りMinGWを使用した場合にstd::random_deviceが毎回同じ値を出力するという現象に遭遇したので自分へのメモとして書き残しておきます。

環境

MinGW
コンパイラのバージョン
8.PNG

プログラム

test.cpp
#include <iostream>
#include <random>
using namespace std;

int main(void){
    random_device rnd;
    for(int i = 0; i < 10; i++){
        cout << rnd() << endl;
    }
    return 0;
}

実行結果

2.PNG

3回実行してみましたがすべて同じ値になっています。

nanashiさんの
gccをwindowsで使うならstd::random_deviceを使ってはいけない
に書いてあるようにたぶん同じ原因でシードの値が変わってないのかなぁと思います。

なおCygwinやVisualStudio2015を使用した場合は毎回違う値が生成されていました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0