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

Simulation Simplified
2. Bernoulli Distribution
Class Dice03

import edu.rit.util.Random;
public class Dice03
   {
   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;
      for (int trial = 0; trial < N; ++ trial)
         {
         do
            {
            ++ win;
            }
         while (prng.nextDouble() < 5.0/6.0);
         }
      System.out.printf ("Average winnings = $%.2f%n",
         ((double) win)/((double) N));
      }

   private static void usage()
      {
      System.err.println ("Usage: java Dice03 <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.