+ 3
Why is output always 41?
I tried the example code for rand function and tried it several times, the output was always 41. Is it a mistake, or am I chosen one, or something?
7 Answers
+ 2
well I ran it 4-5 times and the output is always 41
+ 2
rand() is used to generate random numbers.
If we use rand() function for one time [i.e, cout<<rand();], it will produce a random number which is always 41 based on its algorithm. But if we use this function inside a for loop[i.e, for(x=0;x<=12;x++){cout<<rand()<<"\n";}], it will produce total 13 random numbers (as described by this code) including 41. Every time we use this code, we will gate same random numbers as output.
However if we want to generate random numbers between a desired range, we need to write the code as below-
cout<<(rand()%12);
This code will generate random numbers from 1 to 11. Now if we want to generate random numbers from 1 to 12, simply we just have to add 1 with the previous code as previous code is already able to generate random numbers from 1 to 11. The code will be-
cout<<1+(rand()%12);
0
lol even I get the same outputđ
0
Should have completed the section before asking, first output on rand is always 41. That's a pitty, I already thought, it is some sort of telling me, I'm the chosen one
0
it works based on an algorythm, which starts with 41
0
thanks