I've noticed, that standard rand() function gives different results on Windows and Linux. Ofcourse I've used the same seed number (1234). Here are several first results:
WIN: 4068 213 12761 8758
LIN: 479142414 465566339 961126155 1057886067
My application requires that both platforms produce identical output. What are my options? Is there any good replacement for rand() that meets my requirement ?
thanks
PS. I used MSVC 2008 on Windows7, and gcc 4.1.2 on CentOS 5.5
Explanation : rand() will generate random number from 0 to RAND_MAX, it's modulus with 100 ensures that our result must be between 0 and 99 inclusive.
The reason is for repeatability of results. Let's say your doing some testing using a random sequence and your tests fails after a particular amount of time or iterations. If you save the seed, you can repeat the test to duplicate/debug.
There is no difference between RAND and RANDOM. Both functions are included to ensure portability of existing code that references one or both of them. The intrinsic functions RANDOM_NUMBER and RANDOM_SEED provide the same functionality. You can use SRAND to restart the pseudorandom number generator used by RAND.
@jonas: the rand documentation states "returns a single uniformly distributed random number in the interval (0,1)", so it clearly excludes zero and one.
Boost has a wide range of RNGs, presumably with reproducible behaviour across platforms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With