0

int main () { for (int x = 1; x <= 10; x++) { cout << 1 + (rand() % 6) << endl; } }

Can you guys explain how this rand func gives output between 1 to 6?

17th May 2020, 9:29 AM
Salini Selvam
Salini Selvam - avatar
5 Answers
+ 1
Thank u.. it will be helpful to me
17th May 2020, 11:20 AM
Salini Selvam
Salini Selvam - avatar
0
Rand produces a random number and with modulo 6 you get numbers in range 0 and 5. With adding 1 it is in range 1 and 6
17th May 2020, 9:34 AM
Jnn
Jnn - avatar
0
Thank you
17th May 2020, 9:52 AM
Salini Selvam
Salini Selvam - avatar
0
Explain about srand() function and time().
17th May 2020, 10:03 AM
Salini Selvam
Salini Selvam - avatar
0
If you dont call srand() before rand() it will always produce the same pseudo random numbers because it is deterministic. If you set the seed with srand() the pseudo random number generator has a different starting point and can produce different numbers if the seed is not the same. To have a seed that changes the current time() is often used. To get the current time you call time you call time(NULL). So you would set the seed with srand(time(NULL))
17th May 2020, 10:13 AM
Jnn
Jnn - avatar