title: How do I generate thread-safe uniform random numbers?
body: <p>My program needs to generate many random integers in some range (int min, int max). Each call will have a <em>different</em> range. What is a good (preferably thread-safe) way to do this? The following is not thread-safe (and uses rand(), which people seem to discourage):</p>
  
  <pre><code>int intRand(const int &amp; min, const int &amp; max)
  {
      return (rand() % (max+1-min)) + min;
  }
  </code></pre>
  
  <p>This is <strong>much</strong> slower, but uses <code>&lt;random&gt;</code>:</p>
  
  <pre><code>int intRand(const int &amp; min, const int &amp; max) {
      std::default_random_engine generator;
      std::uniform_int_distribution&lt;int&gt; distribution(min,max);
      return distribution(generator);
  }
  </code></pre>
  
  <p>Something like this is what I'm going for (the changeParameters function doesn't exist though):</p>
  
  <pre><code>int intRand(const int &amp; min, const int &amp; max) {
      static std::default_random_engine generator;
      static std::uniform_int_distribution&lt;int&gt; distribution(0, 10);
      distribution.changeParameters(min, max);
      return distribution(generator);
  }
  </code></pre>
  
  <p>Another option would be to make a wide range on the <code>uniform_int_distribution</code> and then use mod like in the first example. However, I'm doing statistical work, so I want the numbers to come from as unbiased of a distribution as possible (e.g., if the range of the distribution used is not a multiple of (max-min), the distribution will be slightly biased). This is an option, but again, I would like to avoid it.</p>
  
  <p><strong>SOLUTION</strong> This solution comes from the answers by @konrad-rudolph @mark-ransom and @mathk . The seeding of the random number generator is done to suit my particular needs. A more common approach would be to use time(NULL). If you make many threads in the same second, they would then get the same seed though. Even with clock() this is an issue, so we include the thread id. A drawback - <strong>this leaks memory</strong> --- one generator per thread.</p>
  
  <pre><code>#if defined (_MSC_VER)  // Visual studio
      #define thread_local __declspec( thread )
  #elif defined (__GCC__) // GCC
      #define thread_local __thread
  #endif
  
  #include &lt;random&gt;
  #include &lt;time.h&gt;
  #include &lt;thread&gt;
  
  using namespace std;
  
  /* Thread-safe function that returns a random number between min and max (inclusive).
  This function takes ~142% the time that calling rand() would take. For this extra
  cost you get a better uniform distribution and thread-safety. */
  int intRand(const int &amp; min, const int &amp; max) {
      static thread_local mt19937* generator = nullptr;
      if (!generator) generator = new mt19937(clock() + this_thread::get_id().hash());
      uniform_int_distribution&lt;int&gt; distribution(min, max);
      return distribution(*generator);
  }
  </code></pre>