This question has been answered by 666 people and has an average rating of 3.49 (based on 390 ratings)
Which of the following expressions will simulate a dice roll (in other words, the value stored in the variable dice will be either 1, 2, 3, 4, 5 or 6)? You can assume that the seed for the random number generator has already been set to the current time.
Option | Alternative | First answers |
Confirmed answers |
---|---|---|---|
A |
dice = (rand() % 5) + 1; |
158 (23.72%) |
1 (0.83%) |
B |
dice = (rand() % 6) + 1; |
410 (61.56%) |
120 (99.17%) |
C |
dice = (rand() % (6 - 1 + 1)); |
66 (9.91%) |
0 (0.00%) |
D |
dice = (rand() % 1) + 6; |
20 (3.00%) |
0 (0.00%) |
E |
dice = (rand() % 10) - 4; |
12 (1.80%) |
0 (0.00%) |
The following explanation has been provided relating to this question: