What is the standard way to get the state of a C++0x random number generator?

给你一囗甜甜゛ 提交于 2020-01-14 08:57:25

问题


I am trying to learn the new C++0x approach to random number generators (26.5), and implement at C++0x-compliant random number engine (26.5.1.4).

The standard goes into detail on the required interface for seed sequences, and how they can be passed to the constructor or seed functions of engines.

However, I cannot find any standard interface to create or generate a seed sequence from an engine, thereby getting its internal state. Is there one? Or can states only be copied between engines via copy-construction/assignment or copying the initial seed sequence?

If it's not possible, does anyone know what the rationale is (if any) for not providing such an interface?


回答1:


There is no API for generating a seed or seed sequence from an engine's state. However the engine's state can be streamed into an istream, and extracted from an ostream. And the engine's state is EqualityComparable to another engine's state (for same-type engines).

One might inspect the istream generated by streaming an engine to it. However the format of that istream is unspecified.




回答2:


That's my understanding as well. (To be clear: I think that states can only be copied between engines via copy-construction/assignment or constructing with the same initial seed sequence and having generated the same number of pseudo-random numbers.)




回答3:


The internal state doesn't have to look anything like the seed sequence. I think this is close to asking the compiler for the source code from a binary. Not possible.

You can copy the entire engine though, and have the copy regenerate the same sequence once more. Or you can stream the state to a file, and reload it again.



来源:https://stackoverflow.com/questions/5142069/what-is-the-standard-way-to-get-the-state-of-a-c0x-random-number-generator

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