|
Alan Kaminsky
|
|
•
|
|
Department of Computer Science
|
|
•
|
|
Rochester Institute of Technology
|
|
•
|
|
4486 +
2220 =
6706
|
|
Home Page
|
Simulation Simplified
2. Bernoulli Distribution
Class Dice04
import edu.rit.numeric.BernoulliPrng;
import edu.rit.util.Random;
public class Dice04
{
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]);
BernoulliPrng prng =
new BernoulliPrng (Random.getInstance (seed), 5.0/6.0);
int win = 0;
for (int trial = 0; trial < N; ++ trial)
{
do
{
++ win;
}
while (prng.next());
}
System.out.printf ("Average winnings = $%.2f%n",
((double) win)/((double) N));
}
private static void usage()
{
System.err.println ("Usage: java Dice04 <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.