0

Why this is 41?

#include <iostream> #include<cstdlib> using namespace std; int main() { cout<< rand(); return 0; } //* output 41 *//

1st Feb 2017, 6:52 PM
Sayem Mohammad
Sayem Mohammad - avatar
1 Respuesta
+ 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.
1st Feb 2017, 7:03 PM
Alejandro Aristizabal
Alejandro Aristizabal - avatar