Setting seed boost::random

*爱你&永不变心* 提交于 2019-12-04 02:56:54
anton skvorts

Following Jim, Alan and Igor suggestions made some changes to the code: rng.engine().seed() instead of gener.seed(), and called rng.distribution().reset() after the call to rng.engine().seed() and it worked like a charm.

Many thanks!

You should call normal.reset() after the call to gener.seed(). That is specified to ensure that the output of normal will not depend on any previous output from gener.

(The distribution is probably caching some state that you need to clear out.)

I believe boost::variate_generator<> makes a copy of your boost::mt19937 gener object. So when you reseed your copy of gener, it has no effect on the rng object that's already been constructed. Constructing a new rng object each time you reseed should give you the behavior you want (disclaimer: not tested!)

Great to see the problem resolved! But I guess I just figured out why Alan's method wouldn't work...

When writing boost::variate_generator<boost::mt19937&,boost::normal_distribution<> > rng(gener, normal), you didn't create another copy of gener because it's a call by reference, but you did create another copy of normal with the variate_generate.

So instead of normal.reset, which only resets the original normal, you should use rng.distribution().reset(). But you can just keep gener.seed(), which I suspect will have the same effect as rng.engine().seed().

I tested it in my code, and it worked as expected.

Well, just in case anybody would care :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!