1 | package desmoj.core.dist; |
2 | |
3 | import java.util.Random; |
4 | |
5 | /** |
6 | * Linear congruential random generator for uniformly distributed pseudo random |
7 | * numbers configured such that a stream of [0,1] double values is produced. |
8 | * Implements the <code>desmoj.dist.UniformRandomGenerator</code> interface. All |
9 | * <code>Distributions</code> in this package use this random generator by |
10 | * default. The implementation is based on the Java API |
11 | * <code>java.util.Random</code> class' random generator. The Java API Random |
12 | * class uses a 48-bit seed as input to the linear congruential formula. (See |
13 | * Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.) |
14 | * |
15 | * @see desmoj.core.dist.UniformRandomGenerator |
16 | * @see desmoj.core.dist.Distribution |
17 | * @see java.util.Random |
18 | * |
19 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
20 | * @author Tim Lechler |
21 | * |
22 | * Licensed under the Apache License, Version 2.0 (the "License"); you |
23 | * may not use this file except in compliance with the License. You may |
24 | * obtain a copy of the License at |
25 | * http://www.apache.org/licenses/LICENSE-2.0 |
26 | * |
27 | * Unless required by applicable law or agreed to in writing, software |
28 | * distributed under the License is distributed on an "AS IS" BASIS, |
29 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
30 | * implied. See the License for the specific language governing |
31 | * permissions and limitations under the License. |
32 | * |
33 | */ |
34 | public class LinearCongruentialRandomGenerator implements |
35 | desmoj.core.dist.UniformRandomGenerator { |
36 | |
37 | /** |
38 | * The random generator provided by the Java API class |
39 | * <code>java.util.Random</code> |
40 | */ |
41 | protected Random javaAPIRandomGenerator; // the random generator used |
42 | |
43 | /** |
44 | * Creates a DefaultrandomGenerator with seed 42. The value 42 has been |
45 | * taken by pure coincidence. It is not really related to Douglas Adams' |
46 | * "Hitchhikers Guide to the Galaxy" trilogy of five books (so far). |
47 | */ |
48 | public LinearCongruentialRandomGenerator() { |
49 | |
50 | javaAPIRandomGenerator = new Random(42); |
51 | |
52 | } |
53 | |
54 | /** |
55 | * Creates a DefaultrandomGenerator with given value as initial seed. |
56 | * |
57 | * @param seed |
58 | * long : The initial seed of the underlying pseudo random |
59 | * generator |
60 | */ |
61 | public LinearCongruentialRandomGenerator(long seed) { |
62 | javaAPIRandomGenerator = new Random(seed); |
63 | } |
64 | |
65 | /** |
66 | * Returns the next pseudo random uniform [0,1] distributed double value |
67 | * from the stream produced by the underlying pseudo random number |
68 | * generator. |
69 | * |
70 | * @return double : The next pseudo random uniform [0,1] distributed double |
71 | * value |
72 | */ |
73 | public double nextDouble() { |
74 | |
75 | return javaAPIRandomGenerator.nextDouble(); |
76 | |
77 | } |
78 | |
79 | /** |
80 | * Sets the seed for the pseudo random number generator. |
81 | * |
82 | * @param newSeed |
83 | * long : The new initial seed value for the pseudo random number |
84 | * generator |
85 | */ |
86 | public void setSeed(long newSeed) { |
87 | |
88 | javaAPIRandomGenerator.setSeed(newSeed); |
89 | |
90 | } |
91 | } |