0

Following code generates a random number. Using templates, I have modified it to generate the random number between the input range according to the argument type:

#include <iostream>
#include <ctime>
#include <iomanip>
#include <limits>

using namespace std;

template <class T>
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 << generateRandomNumber(0,100);

    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    cout << generateRandomNumber(0.0,99.99);
    return 0;
}

This function is working fine for integer but for double, it is always producing 0.00.

References: C++ random float number generation

Edit: Added return statement in generateRandomNumber()

Community
  • 1
  • 1
techbull
  • 118
  • 2
  • 16
  • 1
    Just use [](http://en.cppreference.com/w/cpp/numeric/random). *Seriously*. Specifically [`std::random_device`](http://en.cppreference.com/w/cpp/numeric/random/random_device), one of the random generators such as `std::default_random_engine`, and [`std::uniform_real_distribution`](http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution). If you need integral vs floating-point distinction, provide an overload with SFINAE qualification or use [`std::generate_canonical`](http://en.cppreference.com/w/cpp/numeric/random/generate_canonical). – WhozCraig Oct 31 '13 at 05:58
  • 1
    Since the C++11 standard, C++ have very good [pseudo-random number generation functionality](http://en.cppreference.com/w/cpp/numeric/random). Use that instead. – Some programmer dude Oct 31 '13 at 05:59
  • @JoachimPileborg: I need to add this functionality to the existing code which do not have access to C++11. :( – techbull Oct 31 '13 at 06:07

3 Answers3

3

You really should use -Wall. If you had, your compiler probably would have told you stuff like:

badrand.cc:11:7: warning: unused variable ‘randNum’ [-Wunused-variable]
badrand.cc:12:1: warning: no return statement in function returning non-void [-Wreturn-type

In other words, your generateRandomNumber never returns a value. Instead, it assigns to the local variable randNum which immediately goes out of scope.

Try:

template <class T>
T generateRandomNumber(T startRange, T endRange)
{
    return startRange + (T)rand()/((T)RAND_MAX/(T)(endRange-startRange));
}

Or, possibly more readably:

template <class T>
T generateRandomNumber(T startRange, T endRange)
{
    return startRange + T(rand()) / T(RAND_MAX) * (endRange - startRange));
}
rici
  • 234,347
  • 28
  • 237
  • 341
2

Watch Stephan T. Lavavej's rand() considered harmful presentation from Going Native 2013 for how and why you should use the C++11 random number generation facilities and avoid rand().

mattnewport
  • 13,728
  • 2
  • 35
  • 39
1

FYI, I get compile errors (include <cstdlib>).

But the real problem is that you're not returning anything from generateRandomNumber.

And I recommend printing a newline for separation.

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <iomanip>
#include <limits>

using namespace std;

template <class T>
T generateRandomNumber(T startRange, T endRange)
{
    return startRange + (T)rand()/((T)RAND_MAX/(T)(endRange-startRange));
}
int main()
{
    srand((unsigned)time(0));

    cout << generateRandomNumber(0,100) << "\n";

    std::cout << std::fixed;
    std::cout << std::setprecision(2);
    cout << generateRandomNumber(0.0,99.99) << "\n";
    return 0;
}
Paul Draper
  • 78,542
  • 46
  • 206
  • 285