Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Dice Frequency Simulation (http://www.velocityreviews.com/forums/t543976-dice-frequency-simulation.html)

 cb17890@yahoo.com 10-13-2007 04:58 AM

Dice Frequency Simulation

Hi I'm new to JAVA Programming and need help with my homework!

The assignment: Design and implement a simple, well-documented JAVA
program that uses random numbers to simulate throwing a pair of dice
and recording the total score thrown. That is, each roll will throw
two dice, each of which can randomly result in a face up value of 1,
2, 3, 4, 5, or 6. The program should add the two values to get a
score for the roll. The score will be between 2(1+1) and 12(6+6).
The program should loop to roll the dice a total of 500 times and
should sum up how many times each score was thrown. That is, how many
of the 500 times was a total of 2 thrown, how many times was a total
of 3 thrown, etc. You will need one accumulator for each of the 11
possible scores. Afer all 500 rolls have been thrown and counted, the
program should report for each possible score (2-12), how many times
it was thrown and what % of the time it was thrown. This program
will have no input. It should produce output similar to the
following:

Total Number Percent
Rolled of Times of Time

So far, I know to use the for loop with the following values: (count =
2; count <= 500; count++) but then I'm stuck. I don't know what to do
next!!

 Patricia Shanahan 10-13-2007 05:05 AM

Re: Dice Frequency Simulation

cb17890@yahoo.com wrote:
> Hi I'm new to JAVA Programming and need help with my homework!
>
> The assignment: Design and implement a simple, well-documented JAVA
> program that uses random numbers to simulate throwing a pair of dice
> and recording the total score thrown. That is, each roll will throw
> two dice, each of which can randomly result in a face up value of 1,
> 2, 3, 4, 5, or 6. The program should add the two values to get a
> score for the roll. The score will be between 2(1+1) and 12(6+6).
> The program should loop to roll the dice a total of 500 times and
> should sum up how many times each score was thrown. That is, how many
> of the 500 times was a total of 2 thrown, how many times was a total
> of 3 thrown, etc. You will need one accumulator for each of the 11
> possible scores. Afer all 500 rolls have been thrown and counted, the
> program should report for each possible score (2-12), how many times
> it was thrown and what % of the time it was thrown. This program
> will have no input. It should produce output similar to the
> following:
>
> Total Number Percent
> Rolled of Times of Time
>
> So far, I know to use the for loop with the following values: (count =
> 2; count <= 500; count++) but then I'm stuck. I don't know what to do
> next!!
>

For general advice on getting started, see

In particular, I suggest reducing the size of the problem and working
through doing it paper and pencil.

java.util.Random has a method nextInt(int) which is useful for dice rolls.

Patricia

 Gordon Beaton 10-13-2007 07:00 AM

Re: Dice Frequency Simulation

On Fri, 12 Oct 2007 21:58:30 -0700, cb17890@yahoo.com wrote:
> So far, I know to use the for loop with the following values: (count
> = 2; count <= 500; count++) but then I'm stuck. I don't know what to
> do next!!

I won't do your homework for you, but wonder how you came up with
those particular loop values. How many iterations are you expecting?

You might start by writing a program that just rolls one die once.

/gordon

--

 Roedy Green 10-13-2007 07:04 AM

Re: Dice Frequency Simulation

On Fri, 12 Oct 2007 21:58:30 -0700, cb17890@yahoo.com wrote, quoted or
indirectly quoted someone who said :

>Hi I'm new to JAVA Programming and need help with my homework!

see http://mindprod.com/jgloss/homework.html

Just posting your assignment and hoping others will do it for you
won't fly very far, partly because you would learn nothing, subverting
your enormous investment in time and money to have a school teach you
Java.

--
The Java Glossary
http://mindprod.com

 Roedy Green 10-13-2007 08:43 AM

Re: Dice Frequency Simulation

On Fri, 12 Oct 2007 21:58:30 -0700, cb17890@yahoo.com wrote, quoted or
indirectly quoted someone who said :

>Hi I'm new to JAVA Programming and need help with my homework!

see http://mindprod.com/jgloss/randomnumbers.html
--
The Java Glossary
http://mindprod.com

 rossum 10-13-2007 10:17 AM

Re: Dice Frequency Simulation

On Fri, 12 Oct 2007 21:58:30 -0700, cb17890@yahoo.com wrote:

>Hi I'm new to JAVA Programming and need help with my homework!
>
>The assignment: Design and implement a simple, well-documented JAVA
>program

If you have covered documentation comments /** ... */ then use those
as well.

>that uses random numbers to simulate throwing a pair of dice
>and recording the total score thrown.

Look up java.util.Random in the Java documentation.

>That is, each roll will throw
>two dice, each of which can randomly result in a face up value of 1,
>2, 3, 4, 5, or 6.

Stop here and write a program to simulate rolling a single six-sided
die and print out the result. Test your program and make sure it
works correctly, no rolls of 0, no rolls of 7, no rolls of -3629533.
When it works correctly you can proceed. Do not proceed until you
have go it working right.

You will build up your homework by adding to this initial program so
you need to be sure it works correctly now. It is easier to find the
errors in a small program than in a large one. If you get problems

>The program should add the two values to get a
>score for the roll.
>The score will be between 2(1+1) and 12(6+6).

Now modify your program to roll two dice and add the two values to get
a total score. Again test and make sure that the total score is
always in the range [2, 12], no 1, no 13.

>The program should loop to roll the dice a total of 500 times

Modify your program to roll the pair of dice 500 times. Test and make
sure it is right before you carry on.

>and
>should sum up how many times each score was thrown. That is, how many
>of the 500 times was a total of 2 thrown, how many times was a total
>of 3 thrown, etc. You will need one accumulator for each of the 11
>possible scores.

You will need to think of a way to keep the running counts of the
different scores. Think about which parts of Java you have learned in
class that would be useful to keep a lot of numbers like this. Add
this group of accumulators to your program so that it keeps the
running counts for the 500 die rolls. Make sure that you are
documenting everything. Test again.

>Afer all 500 rolls have been thrown and counted, the
>program should report for each possible score (2-12), how many times
>it was thrown and what % of the time it was thrown. This program
>will have no input. It should produce output similar to the
>following:
>
>Total Number Percent
>Rolled of Times of Time

more code to output the total rolled (2, 3, ... 12) and the number of
times each total was rolled. Test.

Add code to calculate the percentage for each of the different totals
rolled and to display the percentage. Check that your program is
"well-documented". Read through the question again to make sure that
you have not left anything out. Test your program again for the last
time.

Congratulations! You have now completed your homework.

The usual method to tackle a large difficult problem is to start with
a small easy problem related to the large one - in this case we
started by rolling a single die. By building on the small easy
problem we can work up to the large difficult problem in small stages,
testing all the way. Testing is important, as you probably noticed.
If there is a bug in an early stage it will still be there in the
later stages and will be more difficult to find.

>So far, I know to use the for loop with the following values: (count =
>2; count <= 500; count++) but then I'm stuck. I don't know what to do
>next!!

Your code so far has errors in it - remember the importance of testing
roll of one die. Work up to 500 double rolls in steps.

rossum

 Andrew Thompson 10-13-2007 10:31 AM

Re: Dice Frequency Simulation

rossum wrote:
>>Hi I'm new to JAVA Programming and need help with my homework!
>>
>>The assignment: Design and implement a simple, well-documented JAVA
>>program

(snip long, detailed post)

Wow! Except for being more specific to the OP's problem,
that post sounded strongly reminiscent of the advice Patricia
sets out in her 'beginner' document, ..perhaps combined with
the doc. on debugging*.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200710/1

 hwdoer01@gmail.com 10-13-2007 02:20 PM

Re: Dice Frequency Simulation

how com i dont get unifrom distrobution wit this

import java.util.Random;

public class DiceRollerSimulation
{
public static void main(String[] args)
{
Random dieOne = new Random();
Random dieTwo = new Random();
int[] rollMap = new int[11];
for(int i = 0; i < 500; i++)
{
rollMap[dieOne.nextInt(6) + dieTwo.nextInt(6)]++;
}
for(int out = 2; out < 13; out++)
{
System.out.println(out + " " + rollMap[out - 2] + " " +
Math.round((((double)rollMap[out - 2]/500)) * 100.0));
}
}
}

 Joshua Cranmer 10-13-2007 03:16 PM

Re: Dice Frequency Simulation

hwdoer01@gmail.com wrote:
> how com i dont get unifrom distrobution wit this

Since this is a mathematical observation, it won't give anything away if
I just explain it to you:

There are 36 possible combinations:
1,1 1,2 1,3 1,4 1,5 1,6
2,1 2,2 2,3 2,4 2,5 2,6
3,1 3,2 3,3 3,4 3,5 3,6
4,1 4,2 4,3 4,4 4,5 4,6
5,1 5,2 5,3 5,4 5,5 5,6
6,1 6,2 6,3 6,4 6,5 6,6

Sum them up:
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
6 7 8 9 10 11
7 8 9 10 11 12

Question: Is this a uniform distribution?

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

 Gordon Beaton 10-13-2007 03:17 PM

Re: Dice Frequency Simulation

On Sat, 13 Oct 2007 14:20:36 -0000, hwdoer01@gmail.com wrote:
> how com i dont get unifrom distrobution wit this

Why do you expect a uniform distribution? There's only one way to get
2, but 6 ways to get 7.

/gordon

--

All times are GMT. The time now is 12:59 PM.