+ 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?

18th Jul 2017, 2:42 PM
Rohnek Kdosi
7 Answers
+ 2
well I ran it 4-5 times and the output is always 41
18th Jul 2017, 2:54 PM
_Retr0/-
_Retr0/- - avatar
+ 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);
19th Jul 2017, 2:12 PM
Arnab Bhaumik
Arnab Bhaumik - avatar
0
lol even I get the same output😀
18th Jul 2017, 2:49 PM
_Retr0/-
_Retr0/- - avatar
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
18th Jul 2017, 2:52 PM
Rohnek Kdosi
0
it works based on an algorythm, which starts with 41
18th Jul 2017, 2:56 PM
Rohnek Kdosi
0
thanks
19th Jul 2017, 2:45 PM
Rohnek Kdosi