0
Why this is 41?
#include <iostream> #include<cstdlib> using namespace std; int main() { cout<< rand(); return 0; } //* output 41 *//
1 Answer
+ 2
rand() is a pseudo-random function, and uses a seed to generate different secuences... if the seed is the same, the output of the rand() function will be the same.
in order to have a seemingly random output, you must set the seed to a different value every time you run the program.
you can achieve this with srand() and time()
Just add this line before the cout
srand(time(NULL));
and it should work as expected.