title: C++ Calling Pseudo-Random Number Generator Function within a Loop - Constant Seed Issue
body: <p>I'm new to programming and learning C++ by myself. I managed to write a pseudo-random number generator which uses "Mersenne Twister" engine.</p>
  
  <pre><code>double gen_rand()
  {
   std::mt19937 generator (time(0));
   std::uniform_real_distribution&lt;double&gt; dis(0.0, 1.0);
  
   return dis(generator);
  }
  </code></pre>
  
  <p>When I call this function from a loop within <code>main()</code>, the output is always the same. I'm sure this is because of the seed I'm using in the function body, <code>time(0)</code>. Since I'm supplying the same seed everytime I call <code>gen_rand()</code>, I get the same result.</p>
  
  <p>One way I could find to overcome this problem is to embed the function body into <code>main()</code> instead of using a separate function. But since I might need random numbers elsewhere, an outside function is more useful for me. I saw some people have created headers to achieve similar results but such solutions are well beyond my current level of knowledge.</p>
  
  <p>Is there a way to start the seed before calling an outside function and have the function use this process to generate random numbers?</p>
  
  <p>Below is the loop I use:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;random&gt; 
  #include &lt;ctime&gt;
  
  int main()
  {
   std::vector&lt;double&gt; r_num;
  
   for (int i = 0; i&lt;leg; ++i)
   {
       r_num.push_back(gen_rand());
       std::cout &lt;&lt; r_num[i] &lt;&lt; std::endl;
   }
  }
  </code></pre>