In C++ my random number is always 41.

May 21, 2011 at 08:35:12
Specs: Windows Vista
My problem is with C++ programming.
Whenever I ask for a random number it's always 41.

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
double numb;

numb = rand();

cout << "The number is " << numb <<' ';

return 0;

The output is ALWAYS
"The number is 41."

Is this an error of some kind?

See More: In C++ my random number is always 41.

Report •

May 21, 2011 at 12:29:47
you need to "seed the generator"; prior to calling the rand function, use srand(unsigned)time(NULL)) or such.

rand returns an int not a double as well.

Report •
Related Solutions

Ask Question