views:

770

answers:

7

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?

+4  A: 

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.

greyfade
"Every" is a BIG word. Qualifying this with "Most" instead of "Every" is necessary. Most modern RNG's are designed to work consistently across most families of 32-bit Hardware. But that says nothing about 64-bit machines or rare peculiar hardware.
S.Lott
That's why I said you need to have a copy of rand() you can control or at least predict.
greyfade
Fair enough :-) The only other thing to make sure of is that it's portable in terms of datatypes to all your target architectures.
Matt J
I misread your answer, and have retracted my first comment. My apologies :-)
Matt J
A: 

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.

Perchik
+6  A: 

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!

dmckee
Out of curiosity, why not use MT for crypto? Mind sharing a link? (It's an honest question, I'm not trying to be snarky :-)
Matt J
It's easily predicted from small number of know outputs. There is a link in the wikipedia article.
dmckee
http://en.wikipedia.org/wiki/Mersenne_twister#Application
Greg Rogers
Hmmm..there *doesn't* seem to be a link in the wikipedia article. In that case, I don't have one close to hand. Sorry.
dmckee
The original paper includes some commentary on it. Sec 1.6 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf
Greg Rogers
@Greg: Nice. Thanks.
dmckee
A: 

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

pk
You should qualify this with "a given implementation of rand()". No two PRNGs are guaranteed to produce the same sequence.
greyfade
+1  A: 

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 (e.g. Perlin Noise).

Try these links for starters:

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

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

jheriko
+5  A: 

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.

Ferruccio
Gonna have to say +1 on Boost.Random. Works great and they even have specific deterministic classes.
rlbond
+1  A: 

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" (i.e. 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.

Matt J