How to provide unique seeds for hydraulic fracturing at subsequent process starts?

Summary: I need a simple standalone way to sow my RNG so that the seed is different each time the program starts.

Details:

I often need to run the same program (which does random number calculations like Monte Carlo method, etc.) many times in order to have good statistics on the result. In this case, it is important that the random number generator has a different seed in each run.

I would like to have a simple cross-platform solution for this, which may be contained in the program itself. (Ie I don't want to always worry about the script running each instance of the program with a different seed parameter.)

Note that using time(0) as a seed is not a good solution, because the timer resolution is poor: if several processes start in parallel, they are likely to receive the same seed from time(0) .

Requirements:

  • as simple as possible
  • cross-platform (currently I need it to work only on Windows and Linux, x86 and x64).
  • yourself: you should not rely on a special way to run the program (passing the seed as a parameter from the script launch is too big a problem).
  • I would like to wrap all this in a small library that I can incorporate into any new project with minimal effort and just do something like SeedMyRNG(getSeed());

EDIT:

Although my main question was to do this in C (or C ++), based on the pointers provided in the answer, I found os.urandom() as a Python solution (which is also useful to me).

Related question: How to use / dev / random or urandom in C?

+4
source share
4 answers

Cross-platform is a subjective term. Do you mean โ€œany platformโ€ (you may encounter in the future) or โ€œeach platformโ€ (in the list of supported platforms)? Here is a pragmatic approach that I usually take:

  • Check if you have /dev/urandom ; if so, the seed is from there.

  • On Windows, use CryptGenRandom() .

  • If all else fails, run from time() .

+4
source

You can use dev random on Linux and crypto api on Windows. Write a small library to present a platform-independent interface, and it should do exactly what you want.

+1
source

Check out RandomLib which is a C ++ random number library with good seed support. In particular

 Random r; r.Reseed(); 

forces r to sow with a vector of numbers (from calling to RandomSeed :: SeedVector ()), which is almost certainly unique. This includes time, microseconds, pid, hostid, year.

Less optimally, you can also sow RandomSeed :: SeedWord (), which reads from / dev / urandom, if possible. However, you usually get an initial collision after 2 ^ 16 working with one 32-bit word as your seed. Thus, if your application starts many times, you better use the larger seed space offered by the vector.

Of course, this assumes that you are using a random number generator that can use vector seed. RandomLib offers MT19937 and SFMT19937 that use seeds for vectors.

+1
source

Update on 2014-08-04:

Boost now has a cross-platform implementation, random_device . Here's an example for planting a boost pseudo-random generator using unpredictable seed:

 #include <boost/random/mersenne_twister.hpp> #include <boost/random/random_device.hpp> boost::random::mt11213b rng( (boost::random_device())() ); 
0
source

All Articles