I am trying to translate some python code to C++. What the code does is to run a monte carlo simulation. I thought the results from Python and C++ could be very close, but seems something funny happened.
Here is what I do in Python:
self.__length = 100
self.__monte_carlo_array=np.random.uniform(0.0, 1.0, self.__length)
Here is what I do in C++:
int length = 100;
std::random_device rd;
std::mt19937_64 mt(rd());
std::uniform_real_distribution<double> distribution(0, 1);
for(int i = 0; i < length; i++)
{
double d = distribution(mt);
monte_carlo_array[i] = d;
}
I ran above random number generation 100x5 times both in Python and C++, and then do monte carlo simulation with these random numbers.
In monte carlo simulation, I set the threshold as 0.5, thus I can easily verify if the results are uniform distributed.
Here is a conceptual draft what monte carlo simulation does:
for(i = 0; i < length; i++)
{
if(monte_carlo_array[i] > threshold) // threshold = 0.5
monte_carlo_output[i] = 1;
else
monte_carlo_output[i] = 0;
}
Since the length of the monte carlo array is 120, I expect to see 60 1
s both in Python and C++. I calculate the average number of 1
s and found that, although the average number in C++ and Python is around 60, but the trend are highly correlated. Moreover, the average number in Python is always higher than in C++.
May I know if this is because I've done something wrong, or it is simply because the difference between random generation mechanisms in C++ and Python?
[edit] Please note that the RNG in Python is also the Mersenne Twister 19937.
[52,68]
assuming Poissonian uncertainties). Yes, there are obviously differences between the numbers. But you initialize both your RNGs with a random seed, what did you expect? Also, what exactly does the plot show? If you just count the ones, how do you end up withnumbers like 59.75 (Python, run 4)? – Carsten