title: Using same random number generator across multiple functions
body: <p>I am given to believe that random number generators (RNGs) should only be seeded once to ensure that the distribution of results is as intended.</p>
  
  <p>I am writing a Monte Carlo simulation in C++ which consists of a main function ("A") calling another function ("B") several times, where a large quantity of random numbers is generated in B.</p>
  
  <p>Currently, I am doing the following in B:</p>
  
  <pre><code>void B(){
      std::array&lt;int, std::mt19937::state_size&gt; seed_data;
      std::random_device r;
  
      std::generate(seed_data.begin(), seed_data.end(), std::ref(r));
      std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); //perform warmup
      std::mt19937 eng(seq);
  
      std::uniform_real_distribution&lt;&gt; randU(0,1);
  
      double myRandNum = randU(eng);
  
      //do stuff with my random number
  }
  </code></pre>
  
  <p>As you can see, I am creating a new random number generator each time I call the function B. This, as far as I can see, is a waste of time - the RNG can still generate a lot more random numbers!</p>
  
  <p>I have experimented with making "eng" extern but this generates an error using g++:</p>
  
  <p>error: ‘eng’ has both ‘extern’ and initializer extern std::mt19937 eng(seq);</p>
  
  <p>How can I make the random number generator "global" so that I can use it many times?</p>