Alan Kaminsky Department of Computer Science Rochester Institute of Technology 4486 + 2220 = 6706
Home Page

Simulation Simplified
1. A Dice Game
Class Dice02

import edu.rit.util.Random;
public class Dice02
   {
   public static void main
      (String[] args)
      throws Exception
      {
      if (args.length != 2) usage();
      int N = Integer.parseInt (args[0]);
      long seed = Long.parseLong (args[1]);
      Random prng = Random.getInstance (seed);
      int win = 0;
      int die;
      for (int trial = 0; trial < N; ++ trial)
         {
         do
            {
            ++ win;
            die = prng.nextInt (6) + 1;
            }
         while (die != 6);
         }
      System.out.printf ("Average winnings = $%.2f%n",
         ((double) win)/((double) N));
      }

   private static void usage()
      {
      System.err.println ("Usage: java Dice02 <N> <seed>");
      System.exit (1);
      }
   }
Alan Kaminsky Department of Computer Science Rochester Institute of Technology 4486 + 2220 = 6706
Home Page
Copyright © 2011 Alan Kaminsky. All rights reserved. Last updated 31-Aug-2011. Please send comments to ark­@­cs.rit.edu.