Velocity Reviews > Java > Dice Frequency Simulation

# Dice Frequency Simulation

rossum
Guest
Posts: n/a

 10-13-2007
On Sat, 13 Oct 2007 14:20:36 -0000, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>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

Jeff Higgins
Guest
Posts: n/a

 10-13-2007

rossum wrote:
> 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

Roedy Green
Guest
Posts: n/a

 10-14-2007
On Sat, 13 Oct 2007 14:20:36 -0000, (E-Mail Removed) wrote, quoted
or indirectly quoted someone who said :

>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?
--
The Java Glossary
http://mindprod.com

Roedy Green
Guest
Posts: n/a

 10-14-2007
On Sun, 14 Oct 2007 09:42:50 GMT, Roedy Green
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :

>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.
--
The Java Glossary
http://mindprod.com