Cross-platform reproducible number generator

I need a "random" number generator that gives the same result for a given seed on Windows, Mac, Linux, iOS, and Android. Now I've tried std::rand, and boost::random_int_generatorwith the help boost::mt19937, but unfortunately, the result is different from Windows and Mac.

Does anyone know of an implementation (C ++) that works reliably on all platforms?

EDIT 1:

To be more specific, the difference between the numbers from boost::mt19937Windows and Mac shows that Windows has (2) additional blocks of numbers generated. This looks really weird because most of the numbers are the same, since these blocks are only present on Windows.

EDIT 2:

boost::mt19937It works reliably on all platforms. Our problems were not a mistake.

+4
source share
3 answers

In different numbers the code fragment glmthat we used is given. They use an indefinite order of evaluation of arguments, which is good for almost random purposes, but not when you want deterministic numbers (obviously). Therefore, we adjusted the code for our purposes and successfully used it boost::mt19937for Windows, Mac, Linux, Android and iOS.

Sorry for the confusion.

+2
source

RNG, : https://en.wikipedia.org/wiki/Linear_congruential_generator , .

, (uint32_t ..), .

, (https://en.wikipedia.org/wiki/Mersenne_Twister) , .

- AES ( -, , Chacha20) CTR ( ) PRNG; () :-). , AES ( ).

EDIT: - PRNG :

class CryptoBasedPRNG {

 uint128_t key;
 uint128_t count;

 CryptoBasedPRNG(whatever-type seed) {
   //derive key and initial counter from seed
   //  we'll be using SHA256, but any other split should do (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' as input data for aes128 (in ECB mode, if anybody asks about mode at this point)
  }
}

.

+3

You might want to try PCG-Random, from http://www.pcg-random.org/

decent, fast, portable

+2
source

All Articles