简体   繁体   中英

Saving the state of a random number generator in haskell

I'm currently using the Crypto.Random and Crypto.Random.DRBG Haskell libraries for generating random numbers. However, I find myself needing to store their states in some form of file (eg a .txt file), since I want to continue using the same generator in some later point in time (this because I need two separate programs to generate the same random numbers in the same order). The above mentioned libraries are not instances of the Show function. Does anyone know how I could store the state of the mentioned generator?

You can perform serialization of CtrDRBG using the git version of DRBG (since commit 9da47c4dabf9f6976edcbf938c4a1a4b95e09b0c). EDIT: this is now on hackage. To Install:

cabal update
cabal install DRBG

And now you should be able to use Data.Serialize 's encode and decode functions on CtrDRBG .

EDIT: I feel like I should mention that, like all NIST SP 800-90 generators, DRBG might not give the properties you might expect. It is true that the generator is deterministic, but your requests perturb the state so generating N bytes twice does not result in the same bytes as a single request to generate 2*N bytes. This is due to the backtracking resistance of the generators. A buffering strategy that always makes consistent-sized requests and performs concatenation under the covers could hide this behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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