Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different rand() results on Windows and Linux

Tags:

c++

random

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

like image 999
michal Avatar asked Oct 18 '10 11:10

michal


People also ask

What output would rand () generate?

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.

Why does rand () give the same value?

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.

What is the difference between Rand and random?

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.

Does rand () include 1?

@jonas: the rand documentation states "returns a single uniformly distributed random number in the interval (0,1)", so it clearly excludes zero and one.


1 Answers

Boost has a wide range of RNGs, presumably with reproducible behaviour across platforms.

like image 64
Marcelo Cantos Avatar answered Oct 22 '22 12:10

Marcelo Cantos