go to previous page   go to home page   go to next page hear noise

Answer:

Logically, no. But it is often convenient. Each object of a simulation might contain its own random number generator. If you were simulating tadpoles swimming about randomly in a pond, you would probably have a random number generator as part of each tadpole object.


Two-dice Program

two dice

Here is a program that simulates the toss of two dice:

import java.util.*;

public class TwoDieToss
{

  public static void main ( String[] args )
  {
    Scanner scan = new Scanner( System.in );    
    Random rand = new Random();
    
    while ( true )
    {
      System.out.print("You toss a " + (rand.nextInt(6)+1 + rand.nextInt(6)+1) );
      String input = scan.nextLine();
    }
  }
  
}

The two tosses and the sum of the spots is implemented in the expression:

(rand.nextInt(6)+1 + rand.nextInt(6)+1)

Each call to nextInt(6) is completely independent of the previous call, so this is the same as throwing two independent dice.

Throwing one die twice and adding up each outcome is equivalent to throwing two dice and adding up each die. However, throwing a 12-sided die once is not equivalent to throwing two 6-siced dice.


QUESTION 9:

Would the following work to simulate two dice:

(rand.nextInt(11)+2)