title: C++ generating random numbers in a loop using default_random_engine
body: <p>Recently I was trying out a program on generating random numbers in C++ using the <code>random engines</code> defined in <code>#include&lt;random&gt;</code>. My program goes as follows :-</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;random&gt;
  #include &lt;chrono&gt;
  using namespace std;
  int random (int lim)
  {
      default_random_engine dre (chrono::steady_clock::now().time_since_epoch().count());
      uniform_int_distribution&lt;&gt; uid(1,lim);
      return uid(dre);
  }
  int main()
  {
      for (int i=0;i&lt;10;++i)
      cout&lt;&lt;random(100)&lt;&lt;" ";
      return 0;
  }
  </code></pre>
  
  <p>Simple, definitely ! But when I tried the output the numbers were less random like these :-</p>
  
  <pre><code>66 95 95 96 96 96 96 96 97 97
  </code></pre>
  
  <p>When I changed my program slightly &amp; declared the <code>default_random_engine</code> as <code>static</code> or made it <code>global</code> then my output was proper such as this :-</p>
  
  <pre><code>62 53 21 38 7 51 46 40 86 12
  </code></pre>
  
  <p>Could anyone point out what actually was going wrong in my program initially &amp; how did the small change help me get a better output ?</p>