使用gcc 4.8.5与gcc 8.1.0进行编译时,以下测试代码会产生不同的结果:
// test.cpp
#include <algorithm>
#include <random>
#include <iostream>
int main(){
std::mt19937_64 rand_gen(1);
int n_combos = 5;
std::vector<size_t> indices(n_combos);
for (size_t i=0; i<n_combos; ++i)
indices[i] = i;
std::shuffle(indices.begin(), indices.end(), rand_gen);
for(auto elem: indices)
std::cout << elem << std::endl;
}
GCC 4.8.5:
[brock@localhost]$ /usr/bin/g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[brock@localhost]$ /usr/bin/g++ -std=c++11 test.cpp -o printrandom.exe
[brock@localhost]$ ./printrandom.exe
4, 3, 1, 0, 2,
GCC 8.1.0:
[brock@localhost]$ g++ --version
g++ (GCC) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[brock@localhost]$ g++ -std=c++11 test.cpp -o printrandom.exe
[brock@localhost]$ ./printrandom.exe
3, 0, 4, 2, 1,
为什么对于相同的种子编号生成器它们会产生不同的结果,和/或如何找出它们为何不同的原因?我怀疑某个地方的实现存在差异。