Programming

You are logged in as xxxx. Logout

Question

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.

Alternatives

The contributor suggests B is the correct option
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%)

Explanation

The following explanation has been provided relating to this question:

The rand() function returns a random number, and computing modulo 6 will generate a value bewteen 0 and 5 inclusive.

Administrator comment

As an administrator, you can choose to write a comment about this question that will be visible to all students. This comment will appear above all student comments and will be clearly highlighted.

Add administrator comment

Comments

There are 3 comments for this question (1 top-level comment and 2 replies) As an administrator for this course, you can write comments about this question. Any comments you write will appear just like student comments. If you would like to write a special comment that is labelled as an "Administrator comment", please use the option above.
Written: 11:17pm, 26 SepAuthor has: 580 points and 9 badges

Hi, can someone please clarify what is rand ()? I know is it a random number but do you mean the number that is rolled by dice?  Thanks. 

 Written: 8:24am, 27 SepAuthor has: 644 points and 12 badges
 

we haven't been taught rand yet, but the reason it's 6 is because the only possible reamainders of ANY number divided by 6 are 0,1,2,3,4,5. You can't have a remainder of 6 because remainder refers to the amound left over when it's divided by 6. It would go back to 0 in this case

The perk of this is that no matter what the maximum rand number is (e.g. 2376287), we can get the number of possibilities we want.

In this case, he means the number rolled by the dice, so we need to + 1 because we want the number to be from 1 to 6, not 0 to 5.

I hope this helps!

 Written: 5:19pm, 28 SepAuthor has: 429 points and 5 badges
 

the rand function produces a random number between 1 and RAND_MAX , I think its the max 32 bit integer value something like 32000, mod-ing it with 6 will produce values that range from 0-5 so we add one one toresult to get the desired range.

  Add a reply to this comment

<< Prev | 1 | Next >>
(Displaying 1 - 1 of 1)

Write a new comment

Edit this question

If you edit the question, this current version will be archived and a new version of the question will be created (this new version will still appear as being authored by the original author, however all ratings, comments and responses will be reset).

Edit this question

Delete this question

If you feel this question is inappropriate for this course, you can delete it from the system by pressing the button below. All associated ratings and feedback comments will also be deleted if you choose to remove this question.