SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BinomialConvolutionGen.java
1/*
2 * Class: BinomialConvolutionGen
3 * Description: binomial random variate generators using the convolution method
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.randvar;
26
27import umontreal.ssj.probdist.*;
28import umontreal.ssj.rng.*;
29
41public class BinomialConvolutionGen extends BinomialGen {
42
48 public BinomialConvolutionGen(RandomStream s, int n, double p) {
49 super(s, null);
50 setParams(n, p);
51 }
52
58 super(s, dist);
59 }
60
61 public int nextInt() {
62 int x = 0;
63 for (int i = 0; i < n; i++) {
64 double unif = stream.nextDouble();
65 if (unif <= p)
66 x++;
67 }
68 return x;
69 }
70
76 public static int nextInt(RandomStream s, int n, double p) {
77 if (n <= 0)
78 throw new IllegalArgumentException("n <= 0");
79 if (p < 0 || p > 1)
80 throw new IllegalArgumentException("p must be in [0,1]");
81 return convolution(s, n, p);
82 }
83
84 private static int convolution(RandomStream stream, int n, double p) {
85 int x = 0;
86 for (int i = 0; i < n; i++) {
87 double unif = stream.nextDouble();
88 if (unif <= p)
89 x++;
90 }
91 return x;
92 }
93}
Extends the class DiscreteDistributionInt for the binomial distribution slaw00a  (page 321) with para...
BinomialConvolutionGen(RandomStream s, int n, double p)
Creates a binomial random variate generator with parameters.
int nextInt()
Generates a random number (an integer) from the discrete distribution contained in this object.
static int nextInt(RandomStream s, int n, double p)
Generates a new integer from the binomial distribution with parameters.
BinomialConvolutionGen(RandomStream s, BinomialDist dist)
Creates a random variate generator for the binomial distribution dist and stream s.
BinomialGen(RandomStream s, int n, double p)
Creates a binomial random variate generator with parameters and , using stream s.
void setParams(int n, double p)
Sets the parameter and of this object.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...