/***********************************
Program: Question 7.17
Date: February 9, 2010
***********************************/
import java.util.Random;
public class Q717
{
public static void main (String[] args)
{
int die[] = new int [2]; // Two die to roll
int total = 0; // Stores addition of dice
int probability[] = new int [12]; // Number of possibilities
int amount = 36000;
Random dice; // Random dice generator
for (int i = 0 ; i < probability.length ; i++) // For each possibility
probability [i] = 0; // Sets counter to 0
for (int i = 0 ; i < amount ; i++) // Counts 36,000 times
{
dice = new Random ();
die [0] = 1 + dice.nextInt (6); // New random instance
die [1] = 1 + dice.nextInt (6); // New random instance
total = die [0] + die [1]; // Add the results
probability [total - 1] += 1; // Increases counter on probability
}
// Graphical output
System.out.println ("Value\t|\tChances of Rolling Value ( /" + amount + " )");
System.out.println ("---------------------------------------------------");
for (int i = 1 ; i < probability.length ; i++)
System.out.println ((i + 1) + "\t|\t" + probability [i - 1]);
}
}
|