简体   繁体   中英

Deterministic Random Number Streams in C++ 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?

There are dozens of PRNG s 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!

srand() & rand() are not part of the STL. They're actually part of the C runtime. Yes, they will produce the same results as long as it's the same implementation of srand()/rand() .

Depending on your needs, you might want to consider using Boost.Random . It provides several high-quality random number generators.

Assuming the implementations of rand() are the same, yes.

The easiest way to ensure this is to include a known rand() implementation with your program - either included in your project's source code or in the form of a library you can manage.

Write your own pseudorandom number routine. There are a lot of algorithms documented on the internet, and they have a number of applications where rand isn't good enough (eg Perlin Noise ).

Try these links for starters:

http://en.wikipedia.org/wiki/Linear_congruential_generator

http://en.wikipedia.org/wiki/Pseudorandom_number_generator

No, the ANSI C standard only specifies that rand() must produce a stream of random integers between 0 and RAND_MAX, which must be at least 32767 ( source ). This stream must be deterministic only in that, for a given implementation on a given machine, it must produce the same integer stream given the same seed.

You want a portable PRNG. Mersenne Twister (many implementations linked at the bottom) is pretty portable, as is Ben Pfaff's homegrown C99-compliant PRNG . Boost.Random should be fine too; as you're writing your code in C++, using Boost doesn't limit your choice of platforms much (although some "lesser" (ie non-compliant) compilers may have trouble with its heavy use of template metaprogramming). This is only really a problem for low-volume embedded platforms and perhaps novel research architectures, so if by "any computer" you mean "any x86/PPC/ARM/SPARC/Alpha/etc. platform that GCC targets", any of the above should do just fine.

I believe if you supply with srand with the same seed, you will get the same results. That's pretty much the definition of a seed in terms of pseudo random number generators.

Yes. For a given seed (starting value), the sequence of numbers that rand() returns will always be the same.

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