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

Simulation Simplified
3. Gathering and Displaying Data
Class Dice06

import edu.rit.numeric.BernoulliPrng;
import edu.rit.numeric.plot.Plot;
import edu.rit.util.Random;
public class Dice06
   {
   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);
      double[] wincount = new double [maxwin + 1];
      for (int trial = 0; trial < N; ++ trial)
         {
         int win = 0;
         do
            {
            ++ win;
            }
         while (prng.next());
         wincount[Math.min (win - 1, maxwin)] += 1;
         }
      double[] winamount = new double [maxwin + 1];
     for (int i = 0; i <= maxwin; ++ i) winamount[i] = i + 1;
      new Plot()
         .plotTitle ("Dice Game")
         .xAxisTitle ("Winnings")
         .yAxisTitle ("Occurrences")
         .xySeries (winamount, wincount)
         .getFrame()
         .setVisible (true);
      }

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