Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deterministic Random Number Streams in C++ STL

Tags:

c++

random

stl

I want to supply a number, and then receive a set of random numbers. However, I want those numbers to be the same regardless of which computer I run it on (assuming I supply the same seed).

Basically my question is: in C++, if I make use of rand(), but supply srand() with a user-defined seed rather than the current time, will I be able to generate the same random number stream on any computer?

like image 909
Kurisu Avatar asked Mar 16 '09 19:03

Kurisu


People also ask

What is deterministic random number generator?

The DRBG produces a sequence of bits from a secret initial value called a seed. A cryptographic DRBG has the additional property that the output is unpredictable given that the seed is not known. A DRBG is sometimes also called a pseudo-random number generator (PRNG) or a deterministic random number generator.

What is std :: mt19937?

std::mt19937(since C++11) class is a very efficient pseudo-random number generator and is defined in a random header file. It produces 32-bit pseudo-random numbers using the well-known and popular algorithm named Mersenne twister algorithm. std::mt19937 class is basically a type of std::mersenne_twister_engine class.

Is random seed deterministic?

The seed is the point of this extremely long sequence where the generator starts. So yes, it is deterministic. A pseudo-random number generator is an endlessly repeating fixed list of numbers.

Is C# random deterministic?

All of this is deterministic. If you start off an instance of Random with the same initial state (which can be provided via a seed) and make the same sequence of method calls on it, you'll get the same results.


1 Answers

There are dozens of PRNGs available as libraries. Pick one. I tend to use Mersenne Twister.

By using an externally supplied library, you bypass the risk of a weird or buggy implementation of your language's library rand(). As long as your platforms all conform to the same mathematical semantics, you'll get consistent results.

MT is a favorite of mine because I'm a physicist, and I use these things for Monte Carlo, where the guarantee of equal-distribution to high dimensions is important. But don't use MT as a cryptographic PRNG!

like image 153
dmckee --- ex-moderator kitten Avatar answered Oct 02 '22 04:10

dmckee --- ex-moderator kitten