1 /*
2 * Copyright (C) 2002-2003, Mark Diggory
3 *
4 * This file is part of the Repast Taglibrary Package for use with Jelly.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. License
19 * information is also available at http://www.gnu.org.
20 *
21 * @author Mark Diggory <mdiggory@latte.harvard.edu>
22 */
23 package org.mrd.random;
24
25 import cern.jet.random.*;
26 import edu.cornell.lassp.houle.RngPack.RandomElement;
27
28 /***
29 * Constructs a Bernoulli Trial with the given successProbability, using a
30 * {@link cern.jet.random.engine.MersenneTwister} seeded with the given seed.
31 * @author Mark R. Diggory
32 * @version 0.1
33 */
34 public class Bernoulli extends AbstractContinousDistribution{
35
36 protected double successProbability;
37
38 /***
39 * Constructs a Bernoulli Trial with the given successProbability, using a {@link cern.jet.random.engine.MersenneTwister} seeded with the given seed.
40 */
41 public Bernoulli(double p, int seed) {
42 super();
43 setRandomGenerator(new cern.jet.random.engine.MersenneTwister(seed));
44 setSuccessProbability(p);
45 }
46 /***
47 * Constructs a Bernoulli Trial with the given successProbability, using a given Random Generator.
48 */
49 public Bernoulli(double p, RandomElement randomGenerator) {
50 super();
51 setRandomGenerator(randomGenerator);
52 setSuccessProbability(p);
53 }
54
55 public int nextInt() {
56 if(nextBoolean()) return 1;
57 else return 0;
58 }
59
60 /***
61 * Returns a uniformly distributed random <tt>boolean</tt>.
62 */
63 public boolean nextBoolean() {
64 return randomGenerator.raw() <= successProbability;
65 }
66
67 public double nextDouble() {
68 return (double) nextInt();
69 }
70
71 /***
72 * Returns a Bernoulli with the propability of success equal to
73 * successProbability.
74 * @exception OutOfBoundsException thrown if successProbability
75 * is not between 0 and 1.
76 */
77 public void setSuccessProbability( double successProbability ) throws IndexOutOfBoundsException {
78 if ( ( successProbability >= 0.0 ) && ( successProbability <= 1.0 ) )
79 this.successProbability = successProbability;
80 else
81 throw new IndexOutOfBoundsException( "Probablility must be between 0 and 1" +
82 "Your value was: " + successProbability);
83 }
84
85 public double getSuccessProbability() {
86 return successProbability;
87 }
88
89 }
This page was automatically generated by Maven