What is the standard way to get the state of a C++

2019-06-20 11:43发布

问题:

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.



标签: c++ random c++11