Write a computer program to find the probability that tossing 2 dice will make a total of 8. 
8 comes up when we roll one of the following:
The probability of each event is:
1 1 1  x  =  6 6 36
So the answer is 5/36.
This technique has a fancy name like Monte Carlo Method
The basis of this technique is to use many (say N) computer experiments  each one with a random outcome  to represent what would happen in reality.
By counting the successes among the N experiments, we can obtain valuable information,
However, the "distribution" (i.e., behavior) of the Psuedo random numbers is virtually identical to random numbers.
nextDouble(): Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence. nextInt(int n): Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive), (i.e., between 0 and n1) drawn from this random number generator's sequence. 
So we must construct a Random object and use the object to invoke these method.
import java.util.Random; public class Random1 { public static void main(String[] args) { int i; Random gen = new Random(); /*  gen.nextInt(6) generate random integers between 0 and 5  */ System.out.println("Testing gen.nextInt(6):"); for ( i = 0; i < 10; i++ ) { System.out.println("Random number in [0, 6) = " + gen.nextInt(6) ); } /*  gen.nextDouble() generate random decimal numbers between 0 and 1  */ System.out.println("Testing gen.nextDouble():"); for ( i = 0; i < 10; i++ ) { System.out.println("Random number in [0, 1) = " + gen.nextDouble() ); } } } 
When a 6sided die is casted (tossed), the outcome of the cast is one of the numbers {1, 2, 3, 4, 5, 6}.
When a Nsided die is casted (tossed), the outcome of the cast is one of the numbers {1, 2, ..., N}.
public class Die { ... } 
We need to be able to construct a die with given number of sides
So we need a constructor that take an integer number (# sides):
public class Die { public Die(int numberSides) { .... } } 
We need to be able to cast the die.
So we need a method "cast"  does this method need any information to do its task ?... No... so no parameters...
Does this "cast" method return anything ?... Yes: the outcome of the die roll !
public class Die { public Die(int numberSides) { .... } public int cast() { .... } } 
No more methods are necessary
Programs need information to do their job.
What information does the methods in the Die class need to perform its job ?
NOTE:
the computer can never make the real thing; it can only make a
representation 
the representation must be capable to perform all the tasks
that the real thing can do.
In the case of a die: the die must return a random number between 1 and n when the die is "casted" 
So we need the following instance variables in Die for the Die class to do its job:
import java.util.Random; public class Die { public Die(int numberSides) { .... } public int cast() { .... } private Random generator; private int sides } 
NOTE:
as mentioned before,
the computer can never make the real thing; it can only make a
representation 
the representation must be capable to perform all the tasks
that the real thing can do
A constructor always must initialize every instance variable so that the object can do its job. 
import java.util.Random; public class Die { public Die(int numberSides) { generator = new Random(); // Construct a Random object sides = numberSides; // Remember the # sides } public int cast() { .... } private Random generator; private int sides } 
import java.util.Random; public class Die { public Die(int numberSides) { generator = new Random(); // Construct a Random object sides = numberSides; // Remember the # sides } public int cast() { return(generator.nextInt(sides)); } private Random generator; private int sides } 

Do the following:

Good news: the computer can do this and in a fraction of a sec !!!
Hey, that's why computer science can be useful :)
// Version 1: don't worry about the variable types, // Just use them and figure out later what their types are.... // Don't worry about "Java"; but BE PRECISE ! // Perform the experiment for n times for ( i = 1; i <= n; i++ ) { "Roll a die d until 6 shows and keep a tally on # rolls"; } "Find the average number of rolls in n experiments"; 
(double) totalNumRolls/numTries
The modified program with specifics on how to compute the average:
// Version 1: solve the "compute average problem: // Things will get more like "Java"; and ALWAYS BE PRECISE ! int totalNumRolls = 0; int numTries = 0; for ( i = 1; i <= n; i++ ) { "Roll a die d until 6 shows"; totalNumRolls = totalNumRolls + (# rolls needed)"; numTries++; } System.out.println( (double) totalNumRolls/numTries ); 
Make sure it's a 6sided die !
"Rolling myDie, recording the outcome and keep a running count of the number of times rolled" is done by using:
outcomeOfThisRoll = myDie.cast(); numOfRolls++;
while ( outcomeOfThisRoll != 6 ) { .... }
The modified program with specifics on rolling a die until 6 shows, while keeping a running count on the numbers of rolls is:
// Version 2: solve the "rolling a die until..." problem // Things will get more like "Java"; and ALWAYS BE PRECISE ! int totalNumRolls = 0; int numTries = 0; Die myDie = new Die(6); // construct a 6sided die int numOfRolls; int outcomeOfThisRoll; for ( i = 1; i <= n; i++ ) { numOfRolls = 0; // Reset numOfRolls for next experiment /* ===================================================== Roll the die for the first time in this experiment ===================================================== */ outcomeOfThisRoll = myDie.cast(); numOfRolls++; /* ===================================================== Keep rolling the die until 6 shows ===================================================== */ while ( outcomeOfThisRoll != 6 ) { outcomeOfThisRoll = myDie.cast(); // Roll the die again numOfRolls++; // Keep running count ! } /* ============================================================== Update information to compute the average number of rolls/try ============================================================== */ totalNumRolls = totalNumRolls + numOfRolls; numTries++; } System.out.println( (double) totalNumRolls/numTries ); 
Answer:
int n = generator.nextInt(2); // 0 = heads, 1 = tails