srand((unsigned)time(null)); is not working for making real rnd numbers..any suggestions??
Printable View
srand((unsigned)time(null)); is not working for making real rnd numbers..any suggestions??
Perhaps 'cause noone can't achieve real random numbers with a computer... though it should do just fine for a home programmer... Any code?
You might try using NULL for the srand() command. C++ doesn't recognize it as "null".
One comon mistake is that srand should only be used once.
... and if none of that has helped, post your code.
srand() by itself won't return a random number. You must use srand() and rand() together.
rand() by itself returns pseudo-random numbers. If you just used rand() by itself, it would return the same random numbers each time you ran the program. To "randomize" the rand() function you use the srand() function. This "seeds" or "randomizes" what the rand() function produces. I use the time() function to seed srand() so it is almost completley random.Code:#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int c;
srand( time(0) );
c = 1 + rand() % 100; // A random number from 1-100
cout << "Your random number for today is " << c << endl;
system("PAUSE");
return 0;
}
all had good points but its was the unsigned that was giving the grief.. many thanks to all for spending the time to reply
What he said was right but if you divide by 100 the max int remainder you can have it 99 ;)