+ 1
If the following code should run to giveout random number,Why does it give 41 everytime I run it...
include <iostream> #include <cstdlib> using namespace std; int main() { cout << rand(); }
4 Antworten
+ 6
it's a semi random number, I you want a real random number use this
#include <iostream>
#include <ctime>
int main()
}
srand(time(NULL));
std::cout << rand();
}
because the current time is now the seed of the rng generator
+ 2
What Johan said, but use nullptr(C++11) instead of NULL though, just a good habit to get into :).
He probably didn't use cstdlib because he wasn't required to, my compiler also doesn't require me to include it to use rand, if you do then just include it.
Also keep in mind that you shouldn't use rand to generate numbers if your application really depends on randomness, because rand isn't random enough.
I once did a simulator and it had wrong results just because I used rand.
0
@johanbeginners Would there be no inclusion of cstlib in the help code you gave?
0
Thanx dennis...