0

Plz tell me what does the for loop indicates in random number program!

#include <iostream> #include <cstdlib> using namespace std; int main () { for (int x = 1; x <= 10; x++) { cout << 1 + (rand() % 6) << endl; } }

20th Jan 2017, 6:44 PM
sachin
4 Respostas
+ 3
It simulates 10 dice throws. rand() gives back a random Integer and %6 turns this integer in a number from 0 to 5. By adding one you get random numbers form 1 to 6, so what you would get if you throw a dice. The for loop just generates 10 of these random numbers/dice throws.
20th Jan 2017, 6:49 PM
Robobrine
Robobrine - avatar
+ 2
It will turn 10 times (x between 1 and 10) the for loop and print inside a random number between 0+1 and 5+1 (so between 1 and 6). But I don't understand why you're not just running it in the code playground to see it by yourself...
20th Jan 2017, 6:50 PM
Dorian
+ 1
% is the remaining of int division. ex: 5%6=5 13%6=1 36%6=0 ... So any numbers % 6 will be between 0 to 5. and for will be the number of times it will show a random number...
21st Jan 2017, 7:53 AM
Dorian
0
means it sets the limit?
20th Jan 2017, 6:56 PM
sachin