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?
6 Réponses
+ 1
Thank u.. it will be helpful to me
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
0
Thank you
0
Explain about srand() function and time().
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))