title: Difference between C++ random number generation and Python
body: <p>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.</p>
  
  <p>Here is what I do in Python:</p>
  
  <pre><code>self.__length = 100
  self.__monte_carlo_array=np.random.uniform(0.0, 1.0, self.__length)
  </code></pre>
  
  <p>Here is what I do in C++:</p>
  
  <pre><code>int length = 100;
  std::random_device rd;
  std::mt19937_64 mt(rd());
  std::uniform_real_distribution&lt;double&gt; distribution(0, 1);
  
  for(int i = 0; i &lt; length; i++)
  {
      double d = distribution(mt);
      monte_carlo_array[i] = d;
  }
  </code></pre>
  
  <p>I ran above random number generation 100x5 times both in Python and C++, and then do monte carlo simulation with these random numbers. </p>
  
  <p>In monte carlo simulation, I set the threshold as 0.5, thus I can easily verify if the results are uniform distributed.</p>
  
  <p>Here is a conceptual draft what monte carlo simulation does:</p>
  
  <pre><code>for(i = 0; i &lt; length; i++)
  {
      if(monte_carlo_array[i] &gt; threshold)    // threshold = 0.5
          monte_carlo_output[i] = 1;
      else
          monte_carlo_output[i] = 0;
  }
  </code></pre>
  
  <p>Since the length of the monte carlo array is 120, I expect to see 60 <code>1</code>s both in Python and C++. I calculate the average number of <code>1</code>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 <strong>always higher</strong> than in C++.</p>
  
  <p><img src="https://i.stack.imgur.com/tUnIu.jpg" alt="distribution chart">
  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?</p>
  
  <p>[edit]
  Please note that the <a href="http://docs.python.org/2/library/random.html" rel="nofollow noreferrer">RNG in Python</a> is also the Mersenne Twister 19937.</p>