Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Instruction:
Complete the code which obtains 3000 random dice throws and prints how many sixes were thrown.

Question:
For 'random() * 6 + 1', I am not sure why the random number has to times 6 and plus 1?

Is 'number6++' to add up all the random dice thrown landed in 6?
Is i++ to accumulate all the 'number6' up to 3000?


C#
public class MyProgram
{
    public void start()
    {
        int number6 = 0;
        int num;
        int i = 0;

        while (i <= 3000)
        {
            num = ((int)(Math.random() * 6 + 1));

            if (num == 6)
            {
                number6++;
            }

            i++;

        }
        System.out.println("Number of sixes: " + number6);
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jul-14 0:53am    
The "question" part suggests that this is not your code, as you don't know why this line was written. Is do, ask the one who wrote it.
Better yet, write it yourself, using just first principles. You don't have to understand why someone wrote something, you just need to write it correctly. There is nothing difficult in this problem.
—SA

This is your homework. You would have already taught otherwise it would not have been given to you as homework. If your have doubts, you should consult your teacher first, or discuss with your coursemates. Nevertheless, I will answer your 3 questions.

Quote:
For 'random() * 6 + 1', I am not sure why the random number has to times 6 and plus 1?

The reason is the Math.random() returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. So as it is, it will never return 6, you have to add 1; and it may return 0.0, which is not a valid number, so again add 1. Got it?

Quote:
Is 'number6++' to add up all the random dice thrown landed in 6?

number6 is to store the number of time 6 has been thrown, 'number6++' is the same as 'number6 = number6 + 1'.

Quote:
Is i++ to accumulate all the 'number6' up to 3000?

No, i acts as a counter to keep track of the number of time the dice has been throws.

You should proceed to do your homework yourself. You may refer to some online links for further help, such as Java Tutorial[^]
 
Share this answer
 
v4
Thank you for answering. This is my self study towards to become a programmer. no one can help me because I'm not studying at uni at the moment. Thank you for your help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900