简体   繁体   English

C ++ std :: mt19937和rng状态保存/加载和可移植性

[英]C++ std::mt19937 and rng state save/load & portability

I want to be able to save and load the state of an RNG so I can reproduce the same random values from a given point (application save/snapshot). 我希望能够保存并加载RNG的状态,这样我就可以从给定的点(应用程序保存/快照)重现相同的随机值。

I see there is an operator << and >> overload, which seems to save to a string as a sequence of numbers. 我看到有一个运算符<<和>>重载,它似乎将字符串保存为一系列数字。

  • Is that the best/only way to save? 这是保存的最佳/唯一方式吗? I wouldn't mind just having the fixed-size binary state value not this space separated string thing that I then need to prefix or put delimiters around for my file format. 我不介意只有固定大小的二进制状态值而不是这个空格分隔的字符串,然后我需要为我的文件格式添加前缀或分隔符。
  • Is this at all portable? 这一切都是便携式的吗? eg can I transfer this between different compiler versions, or even between MSVC and GCC to produce the same data set given identically configured distributions (to a small margin of error in the case of floating point, and exact for integer maths)? 例如,我可以在不同的编译器版本之间,甚至在MSVC和GCC之间传输它,以在给定相同配置的分布的情况下生成相同的数据集(在浮点的情况下为小的误差范围,对于整数数学是精确的)?

Yes, operator<< and operator>> are the only way to import or export a random number generator's state. 是的, operator<<operator>>是导入或导出随机数生成器状态的唯一方法。 You can easily convert the textual representation to and from binary, if you'd like. 如果您愿意,可以轻松地将文本表示转换为二进制文件。

De-serializing and serializing mt19937 state should be portable between implementations. 反序列化和序列化mt19937状态应该在实现之间可移植。 The result of reading and writing the engine's state via the streaming operators is well defined by the standard, as long as you ensure the streams are imbued with the same locale. 只要您确保流中充满相同的语言环境,标准就可以很好地定义通过流操作符读取和写入引擎状态的结果。

See § 26.5.1.5 for the requirements of operator<< and operator>> , followed by § 26.5.3.2 for the textual representation of mersenne_twister_engine , which mt19937 is a well defined typedef of. 有关operator<<operator>>的要求,请参见§26.5.1.5,关于mersenne_twister_engine的文本表示 ,请mersenne_twister_engine ,其中mt19937是明确定义的typedef。

On top of the previous answer: 除了上一个答案:

  • exporting textual representation via op<< will only save proper RNG state if it is read back via op>> using the same locale. 通过op <<导出文本表示只有在使用相同的语言环境通过op >>回读时才会保存正确的RNG状态。 Changing locale will cause problem 更改区域设置将导致问题

  • the choice of default_random_engine is implementation defined. default_random_engine的选择是实现定义的。 It is a typedef, but it is allowed to be set to different real engines on different platforms ( § 26.5.5 ). 它是一个typedef,但允许在不同平台上设置为不同的真实引擎(第26.5.5节)。 Thus, using op<< and op>> to save/restore states pretty much prohibits using default engine 因此,使用op <<和op >>来保存/恢复状态几乎禁止使用默认引擎

  • while generators are specified pretty rigorously, I do not believe distributions are required to be identical between platforms. 虽然生成器的指定非常严格,但我不认为平台之间的分布必须相同。 Saving state might not help you much with reproducibility here 在这里,保存状态对可重复性可能没什么帮助

I would recommend to have this document handy, just in case http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf 我建议将这份文件放在手边,以防万一http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM