C++11 std::generate and std::uniform_real_distribution called two times gives strange results

后端 未结 1 723
悲哀的现实
悲哀的现实 2021-01-03 08:02

calling std::generate algorithm from the STL two times on different containers produces equivalent results.

Consider I want to fill up two float arrays with random n

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 08:27

    I believe what's happening here is that std::generate takes its generator argument by value. As soon as you pass it in, it's copied. That means what happens inside std::generate does not affect the function object you passed in.

    So you call generate again, and you copy the same generator function object, which then starts generating with exactly the same state including (somewhere inside there) which ever seed was used to initialise the number generation.

    If you create a DIFFERENT generator function, even with the same parameters, you must get a different random seed inside of it. This is how your other methods are generating different results. Ultimately, their generators start with a different seed.

    0 讨论(0)
提交回复
热议问题