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

Simulation Simplified
3. Gathering and Displaying Data
Class Dice05

import edu.rit.numeric.BernoulliPrng;
import edu.rit.util.Random;
public class Dice05
   {
   public static void main
      (String[] args)
      throws Exception
      {
      if (args.length != 3) usage();
      int maxwin = Integer.parseInt (args[0]);
      int N = Integer.parseInt (args[1]);
      long seed = Long.parseLong (args[2]);
      BernoulliPrng prng =
         new BernoulliPrng (Random.getInstance (seed), 5.0/6.0);
      int[] wincount = new int [maxwin + 1];
      for (int trial = 0; trial < N; ++ trial)
         {
         int win = 0;
         do
            {
            ++ win;
            }
         while (prng.next());
         ++ wincount[Math.min (win - 1, maxwin)];
         }
      System.out.printf ("Win\tCount%n");
      for (int i = 0; i <= maxwin; ++ i)
         {
         System.out.printf ("%d\t%d\n", i + 1, wincount[i]);
         }
      }

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