SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TruncatedRandomStream.java
1/*
2 * Class: TruncatedRandomStream
3 * Description: container random stream generating numbers in an interval
4 (a,b) instead of in (0,1)
5 * Environment: Java
6 * Software: SSJ
7 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
8 * Organization: DIRO, Universite de Montreal
9 * @author
10 * @since
11 *
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 */
26package umontreal.ssj.rng;
27
28import umontreal.ssj.rng.RandomStream;
29
43public class TruncatedRandomStream implements RandomStream {
44 private RandomStream stream;
45 private double a;
46 private double bminusa;
47
48 public TruncatedRandomStream(RandomStream stream, double a, double b) {
49 if (stream == null)
50 throw new NullPointerException("The given stream must not be null");
51 if (a >= b)
52 throw new IllegalArgumentException("a must be smaller than b");
53 if (a < 0 || b < 0 || a > 1 || b > 1)
54 throw new IllegalArgumentException("a and b must be in [0, 1]");
55 this.stream = stream;
56 this.a = a;
57 bminusa = b - a;
58 }
59
60 public void resetStartStream() {
61 stream.resetStartStream();
62 }
63
64 public void resetStartSubstream() {
65 stream.resetStartSubstream();
66 }
67
68 public void resetNextSubstream() {
69 stream.resetNextSubstream();
70 }
71
72 public double nextDouble() {
73 double v = stream.nextDouble();
74 return a + v * bminusa;
75 }
76
77 public void nextArrayOfDouble(double[] u, int start, int n) {
78 stream.nextArrayOfDouble(u, start, n);
79 for (int i = start; i < start + n; i++)
80 u[i] = a + u[i] * bminusa;
81 }
82
83 public int nextInt(int i, int j) {
84 return i + (int) (nextDouble() * (j - i + 1));
85 }
86
87 public void nextArrayOfInt(int i, int j, int[] u, int start, int n) {
88 for (int x = start; x < start + n; x++)
89 u[x] = nextInt(i, j);
90 }
91}
void resetStartStream()
Reinitializes the stream to its initial state : and are set to .
void resetNextSubstream()
Reinitializes the stream to the beginning of its next substream:
void nextArrayOfDouble(double[] u, int start, int n)
Generates n (pseudo)random numbers from the uniform distribution and stores them into the array u sta...
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...
int nextInt(int i, int j)
Returns a (pseudo)random number from the discrete uniform distribution over the integers ,...
void nextArrayOfInt(int i, int j, int[] u, int start, int n)
Generates n (pseudo)random numbers from the discrete uniform distribution over the integers ,...
void resetStartSubstream()
Reinitializes the stream to the beginning of its current substream:
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...