title: Generate random numbers in C++-11 in different parts of a code using the same seed
body: <p>I am trying to encapsulate a class to generate random numbers controlling by seed and by the range of random values. I want to call this class from different parts of my codes, with different ranges, but using the same seed. </p>
  
  <p>Following suggestions of other post (<a href="https://stackoverflow.com/questions/31417957/encapsulated-random-number-generator-in-c-11-using-boost">Encapsulated Random number generator in C++-11 using boost</a>) I implemented the following code:</p>
  
  <p>My Random Number Generator class:</p>
  
  <pre><code>    #include &lt;random&gt;
      #include &lt;iostream&gt;
  
      typedef std::mt19937                     ENG;    // Mersenne Twister
      typedef std::uniform_int_distribution&lt;&gt; iDIST;   // Uniform Integer Distribution
  
      class RNG {
        private:
           ENG eng;
           iDIST idist;
  
        public:
           iDIST::result_type igen() { return idist(eng); }
           RNG(ENG eng_,int imin,int imax)
           : idist(imin,imax)
           {eng = eng_; }
  
      };
  </code></pre>
  
  <p>A function to create an object of RNG class and print the random values:</p>
  
  <pre><code>    void myfunc(ENG eng_,int imin, int imax, int N){
          RNG myirn(eng_,imin,imax);
          for (int i = 0; i &lt; N; i++){
              std::cout &lt;&lt; myirn.igen() &lt;&lt; std::endl;
          }
          return;
      }
  </code></pre>
  
  <p>My main function:</p>
  
  <pre><code>    int main(int argc, char **argv){
          int myseed = 1;
          int N = 5;
          int imin1 = 1;
          int imax1 = 10;
  
      //Seed globally
          ENG eng_;
          eng_.seed(myseed);
  
          std::cout &lt;&lt; "Range = [" &lt;&lt; imin1 &lt;&lt; "," &lt;&lt; imax1 &lt;&lt; "]" &lt;&lt; std::endl;
          myfunc(eng_,imin1,imax1,N);
          std::cout &lt;&lt; "Range = [" &lt;&lt; imin1 &lt;&lt; "," &lt;&lt; imax1 &lt;&lt; "]" &lt;&lt; std::endl;
          myfunc(eng_,imin1,imax1,N);
  
  
      return 0;
      }
  </code></pre>
  
  <p>As you can see, my strategy was to seed my random number generator globally (in the main function) and pass the variable eng_ as a parameter to the function func, that will instantiate the RNG object and print the random values. If everything were correct, this code should print 2 sequences of 5 random numbers within the same range but with different values. However, they are exactly the same sequence. Could anyone help me to fix this? </p>