Dice Frequency Simulation

C

cb17890

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!!
 
P

Patricia Shanahan

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
http://home.earthlink.net/~patricia_shanahan/beginner.html

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
 
G

Gordon Beaton

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

--
 
R

rossum

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

The assignment: Design and implement a simple, well-documented JAVA
program
Can you write comments? "Well documented" means putting in comments.
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
then post your code here and we will help you.
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
Add code to display the specified headings into your program. Add
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
:). As I said, don't start with 500 double rolls, start with a single
roll of one die. Work up to 500 double rolls in steps.

rossum
 
A

Andrew Thompson

rossum said:
Can you write comments? ...
(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*.

* <http://home.earthlink.net/~patricia_shanahan/debug/index.html>

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
H

hwdoer01

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));
}
}
}
 
J

Joshua Cranmer

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?
 
G

Gordon Beaton

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

--
 
R

rossum

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();
Not a good idea, sometimes you will get two random number generators
initialised to the same seed value and you will always get the same
number on the two dice.

Better is:

Random rand = new Random();

rollMap([rand.nextInt(6) + rand.nextInt(6)]++;

In general you only need one instance of Random per program.
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));
}
}
}
As others have pointed out, you should not expect to get a uniform
distribution from rolling two dice and adding the pip values. You can
only make 2 in one way: 1 + 1. There are six ways to make 7: 1 + 6, 2
+ 5, 3 + 4, 4 + 3, 5 + 2 and 1 + 6. You should get roughly six times
as many sevens as you get twos.

rossum
 
J

Jeff Higgins

rossum said:
Better is:

Random rand = new Random();

rollMap([rand.nextInt(6) + rand.nextInt(6)]++;

As others have pointed out,

Good idea. Thanks to all who responded.
hw
 
R

Roedy Green

how com i dont get unifrom distrobution wit this

The sum of two random numbers is no longer uniform.

I gather you never played Monopoly as a child. It is rare to get a 2
or a 12 throwing two dice because there is only one way to get that
sum.

You can get a 7 easily because you can get it with
1 + 6, 2 + 5, 3 + 4 or the reverse.

Do they teach the binomial distribution (which comes to approximate a
Bell shaped normal curve) in your part of the world?
 
R

Roedy Green

The sum of two random numbers is no longer uniform.

I gather you never played Monopoly as a child. It is rare to get a 2
or a 12 throwing two dice because there is only one way to get that
sum.

You can get a 7 easily because you can get it with
1 + 6, 2 + 5, 3 + 4 or the reverse.

Do they teach the binomial distribution (which comes to approximate a
Bell shaped normal curve) in your part of the world?

To convince yourself what I am saying in true, buy or make yourself a
pair of dice with two sugar cubes and a pencil. Throw and record the
sum results in columns, on graph paper using a vertical stroke one
square tall for each hit. You will see a bell shaped pattern emerge.

This manual solution will map nicely onto what you do in Java with an
array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top