title: c++ : Generic Function for random Number generation
body: <p>Following code generates a random number. Using templates, I have modified it to generate the random number between the input range according to the <strong>argument type</strong>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctime&gt;
  #include &lt;iomanip&gt;
  #include &lt;limits&gt;
  
  using namespace std;
  
  template &lt;class T&gt;
  T generateRandomNumber(T startRange, T endRange)
  {
      T randNum = startRange + (T)rand()/((T)RAND_MAX/(T)(endRange-startRange));
      return(randNum);  // It was the ERROR
  }
  int main()
  {
      srand((unsigned)time(0));
  
      cout &lt;&lt; generateRandomNumber(0,100);
  
      std::cout &lt;&lt; std::fixed;
      std::cout &lt;&lt; std::setprecision(2);
      cout &lt;&lt; generateRandomNumber(0.0,99.99);
      return 0;
  }
  </code></pre>
  
  <p>This function is working fine for <strong>integer</strong> but for <strong>double</strong>, it is always producing <strong>0.00</strong>.</p>
  
  <p>References:
  <a href="https://stackoverflow.com/questions/686353/c-random-float-number-generation">C++ random float number generation</a></p>
  
  <p>Edit: Added return statement in generateRandomNumber()</p>