c++ - Crossplatform reproducible number generator -
i need "random" number generator, produces same result given seed on windows, mac, linux, ios , android. tried std::rand
, boost::random_int_generator
boost::mt19937
sadly result different between windows , mac.
does know of (c++) implementation, works reliably on platforms?
edit 1:
to more specific, diff between numbers boost::mt19937
on windows , mac shows, on windows there (2) additional blocks of numbers being generated. looks strange because majority of numbers same these blocks being present on windows.
edit 2:
boost::mt19937
works reliably on platforms. our problems not bug there.
if don't need too-high-quality rng, can implement one-liner according description here: https://en.wikipedia.org/wiki/linear_congruential_generator linear congruential gens got quite bad name recently, many practical purposes they're fine.
as long you're careful using guaranteed-size types (uint32_t etc.), should fine on platforms.
if need better-quality rng, once again can implement mersenne twister (https://en.wikipedia.org/wiki/mersenne_twister) yourself, more complicated.
yet way use aes (or other block cypher matter, chacha20) in ctr mode (using predefined key) prng; of best known (cryptographic) quality :-). won't take coding on side, you'd need link aes implementation (they're available).
edit: example pseudo-code illustrate crypto-based prng:
class cryptobasedprng { uint128_t key; uint128_t count; cryptobasedprng(whatever-type seed) { //derive key , initial counter seed // we'll using sha256, other split should (like odd bits/even bits of seed) uint256_t sha = sha256(seed); key = low_128bits(sha); count = high_128bits(sha); } uint128_t random_128_bits() { count += 1;//with wraparound return aes128(key,count);//encrypting 'count' input data aes128 (in ecb mode, if asks mode @ point) } }
rather easy , random.
Comments
Post a Comment