std::generate() で生成した乱数が毎回同じになるのはなぜ? C++ generate_array() rng ? #include <algorithm> #include <functional> #include <random> #include <iostream> using namespace std; default_random_engine rng; void print_array(const int* ary, size_t n) { for(size_t i = 0; i < n-1; ++i) cout << ary[i] << (i==n-1 ? "" : " "); cout << "\n"; } void generate_array(int* ary, size_t n) { uniform_int_distribution<int> dist; auto gen = bind(dist,rng); generate(ary, ary+n, gen); } int main() { random_device rd; rng.seed(rd()); constexpr int N = 10; int ary[N]; fill(ary, ary+N, 0); print_array(ary, N); generate_array(ary, N); print_array(ary, N); generate_array(ary, N); print_array(ary, N); return 0; } wandbox: < `bind(dist,rng)` `dist(rng)` : <
`std::bind`
`bind(dist, ref(rng))`
xcX3v84RxoQ-4GxG32940ukFUIEgYdPy
bb01b9d1126cb1b73528adab62f1cba4
Stop