How shall I put it,

Are random functions (such as rand() or the ones boost offers) guaranteed to produce the same sequence of numbers on every machine, given that the function is seeded the same?

Thank you!

Printable View

- 01-04-2009gloQuestion about random repetition.
How shall I put it,

Are random functions (such as rand() or the ones boost offers) guaranteed to produce the same sequence of numbers on every machine, given that the function is seeded the same?

Thank you! - 01-04-2009Elysia
That is very likely, yes, if not guaranteed.

Basically, they would compute mathematical formulas on the seed to get their "random" numbers. - 01-04-2009tabstop
Each implementation will give the same value for the same seed, I believe (so any VS2007 implementation should give the same values) -- or at least I'm not aware of any that differ, but it won't be the same from implementation to implementation.

- 01-04-2009glo
This is just what I was hoping to find out.

Thank you! - 01-04-2009matsp
So, to clarify:

As long as it is the same source code and seed, yes. However, bear in mind that in most cases, your C runtime is a shared library (.dll or .so depending on the OS). This means that your application does not contain the actual implementation of the random number generation.

You can (in most cases) make sure that the rand() or similar function is part of your application - this is called "static linking" - but there are drawbacks in the sense that you then get a much larger .exe, and any fixes to improve the C runtime that was compiled into your application will require a recompile - whilst the shared library build would automatically use the newer version if one is installed on the machine.

If you really want to ENSURE this behaviour, implementing your own random number generator (based on known good algorithms - do not attempt to write your OWN algorithm for a random number generator) is probably the safest option. And of course, if you ever want portability between different compilers, different OS's or different C runtime libraries, you will almost certainly have to do your own code.

For a small distribution base, you may simply do the "statically linked C runtime" and be done with it.

--

Mats - 01-04-2009cyberfish
For where you need guaranteed same sequence across different implementations, I would just include a pseudo-random number generator in your code. Something like Mersenne Twister. The official site has a reference implementation, too, that is quite short and only requires <stdio>, and is under a liberal license.

Code:`Redistribution and use in source and binary forms, with or without`

modification, are permitted provided that the following conditions

are met:

1. Redistributions of source code must retain the above copyright

notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright

notice, this list of conditions and the following disclaimer in the

documentation and/or other materials provided with the distribution.

3. The names of its contributors may not be used to endorse or promote

products derived from this software without specific prior written

permission.

- 01-04-2009phantomotap
I'd say to grab one of George Marsaglia's implementations of one of the simpler algorithms and never look back.

Soma - 01-04-2009cyberfish
It's this simple -

http://www.math.sci.hiroshima-u.ac.j...ES/mt19937ar.c

Code:`#include "mt19937.ar.c"`

int main() {

init_genrand(12345);

unsigned int random_number = genrand_int32();

}

- 01-05-2009abachler
- 01-05-2009cyberfishQuote:

There are methods for producing truly random numbers, but they are non-trivial.

- 01-05-2009abachler
Well, software implimentations are never truly random, they are pseudo-random, and fully deterministic by nature.