title: C++ boost random number generator set seed for multiple instances
body: <p>I want to create many instances of a class, and want them to use the boost random number generator to make a normally distributed jump with a user defined mean. I have been reading from some sources they say you don't want to reseed the number generator like <a href="http://www.bnikolic.co.uk/blog/cpp-boost-rand-normal.html" rel="nofollow noreferrer">here</a>. Ideally I wanted a global generator and that each instance of the individual class has the ability to change the mean and generate a random number that is not the same for all instances. I am struggling to implement this I have a global normal class, but the seed is the same for each instance of the class. </p>
  
  <pre><code>// C/C++ standard library
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  
  #include &lt;boost/random/mersenne_twister.hpp&gt;
  #include &lt;boost/random/variate_generator.hpp&gt;
  #include &lt;boost/random/lognormal_distribution.hpp&gt;
  
  /**
   * The mt11213b generator is fast and has a reasonable cycle length
   * See http://www.boost.org/doc/libs/1_60_0/doc/html/boost_random/reference.html#boost_random.reference.generators
   */
  struct random_generator : boost::mt11213b {
      random_generator(void){
          seed(static_cast&lt;unsigned int&gt;(std::time(0)));
      }
  } random_generator;
  
  
  template&lt;
      class Type
  &gt; struct Distribution {
      Type distribution;
      boost::variate_generator&lt;decltype(random_generator),Type&gt; variate_generator;
  
      template&lt;class... Args&gt;
      Distribution(Args... args):
          variate_generator(random_generator,Type(args...)) {}
  
      double random(void) {
          return variate_generator();
      }
  };
  typedef Distribution&lt; boost::normal_distribution&lt;&gt; &gt; Normal;
  
  using namespace std;
  // global normal random number generator
  Normal normal_random_generator;
  
  // Class Individual
  class Individual {
      public:
          Individual() { } // constructor initialise value
          virtual~Individual() = default;
          // an accessor to pass information back
          void move_bias_random_walk(double mu) {
              normal_random_generator = {mu, sigma_};
              distance_ += normal_random_generator.random();
          }
  
          // An accessor for the distance object
          double get_distance() {
              return distance_;
          }
  
      private:
          //containers
          double distance_ = 0.4;
          double sigma_ = 0.4;
  };
  
  
  int main() {
      cout &lt;&lt; "!!!Begin!!!" &lt;&lt; endl;
      // Initialise two individuals in this case but there could be thousands
      Individual individual_a;
      Individual individual_b;
  
      cout &lt;&lt; "starting values: individual a = " &lt;&lt; individual_a.get_distance() &lt;&lt; " individual b = " &lt;&lt; individual_b.get_distance() &lt;&lt; endl;
      // Do 10 jumps with the same mean for each individual and see where they end up each time
  
      cout &lt;&lt; "A\tB" &lt;&lt; endl;
      for (auto i = 1; i &lt;= 10; ++i) {
          double mean = rand();
          individual_a.move_bias_random_walk(mean);
          individual_b.move_bias_random_walk(mean);
          cout &lt;&lt; individual_a.get_distance() &lt;&lt; "\t" &lt;&lt; individual_b.get_distance() &lt;&lt; endl;
      }
      cout &lt;&lt; "finished" &lt;&lt; endl;
  
      system("PAUSE");
      return 0;
  }
  </code></pre>
  
  <p>This is the output you get from compiling the above code.</p>
  
  <pre><code>!!!Begin!!!
  starting values: individual a = 0.4 individual b = 0.4
  A   B
  41.8024 41.8024
  18509.2 18509.2
  24843.6 24843.6
  51344   51344
  70513.4 70513.4
  86237.8 86237.8
  97716.2 97716.2
  127075  127075
  154037  154037
  178501  178501
  finished
  </code></pre>