summaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/math3/random/RandomAdaptor.java
blob: e7030d0b4ed0d9419d869ae812dd9fe33ed6291a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.math3.random;

import java.util.Random;

/**
 * Extension of <code>java.util.Random</code> wrapping a {@link RandomGenerator}.
 *
 * @since 1.1
 */
public class RandomAdaptor extends Random implements RandomGenerator {

    /** Serializable version identifier. */
    private static final long serialVersionUID = 2306581345647615033L;

    /** Wrapped randomGenerator instance */
    private final RandomGenerator randomGenerator;

    /** Prevent instantiation without a generator argument */
    @SuppressWarnings("unused")
    private RandomAdaptor() {
        randomGenerator = null;
    }

    /**
     * Construct a RandomAdaptor wrapping the supplied RandomGenerator.
     *
     * @param randomGenerator the wrapped generator
     */
    public RandomAdaptor(RandomGenerator randomGenerator) {
        this.randomGenerator = randomGenerator;
    }

    /**
     * Factory method to create a <code>Random</code> using the supplied <code>RandomGenerator
     * </code>.
     *
     * @param randomGenerator wrapped RandomGenerator instance
     * @return a Random instance wrapping the RandomGenerator
     */
    public static Random createAdaptor(RandomGenerator randomGenerator) {
        return new RandomAdaptor(randomGenerator);
    }

    /**
     * Returns the next pseudorandom, uniformly distributed <code>boolean</code> value from this
     * random number generator's sequence.
     *
     * @return the next pseudorandom, uniformly distributed <code>boolean</code> value from this
     *     random number generator's sequence
     */
    @Override
    public boolean nextBoolean() {
        return randomGenerator.nextBoolean();
    }

    /**
     * Generates random bytes and places them into a user-supplied byte array. The number of random
     * bytes produced is equal to the length of the byte array.
     *
     * @param bytes the non-null byte array in which to put the random bytes
     */
    @Override
    public void nextBytes(byte[] bytes) {
        randomGenerator.nextBytes(bytes);
    }

    /**
     * Returns the next pseudorandom, uniformly distributed <code>double</code> value between <code>
     * 0.0</code> and <code>1.0</code> from this random number generator's sequence.
     *
     * @return the next pseudorandom, uniformly distributed <code>double</code> value between <code>
     *     0.0</code> and <code>1.0</code> from this random number generator's sequence
     */
    @Override
    public double nextDouble() {
        return randomGenerator.nextDouble();
    }

    /**
     * Returns the next pseudorandom, uniformly distributed <code>float</code> value between <code>
     * 0.0</code> and <code>1.0</code> from this random number generator's sequence.
     *
     * @return the next pseudorandom, uniformly distributed <code>float</code> value between <code>
     *     0.0</code> and <code>1.0</code> from this random number generator's sequence
     */
    @Override
    public float nextFloat() {
        return randomGenerator.nextFloat();
    }

    /**
     * Returns the next pseudorandom, Gaussian ("normally") distributed <code>double</code> value
     * with mean <code>0.0</code> and standard deviation <code>1.0</code> from this random number
     * generator's sequence.
     *
     * @return the next pseudorandom, Gaussian ("normally") distributed <code>double</code> value
     *     with mean <code>0.0</code> and standard deviation <code>1.0</code> from this random
     *     number generator's sequence
     */
    @Override
    public double nextGaussian() {
        return randomGenerator.nextGaussian();
    }

    /**
     * Returns the next pseudorandom, uniformly distributed <code>int</code> value from this random
     * number generator's sequence. All 2<font size="-1"><sup>32</sup></font> possible {@code int}
     * values should be produced with (approximately) equal probability.
     *
     * @return the next pseudorandom, uniformly distributed <code>int</code> value from this random
     *     number generator's sequence
     */
    @Override
    public int nextInt() {
        return randomGenerator.nextInt();
    }

    /**
     * Returns a pseudorandom, uniformly distributed {@code int} value between 0 (inclusive) and the
     * specified value (exclusive), drawn from this random number generator's sequence.
     *
     * @param n the bound on the random number to be returned. Must be positive.
     * @return a pseudorandom, uniformly distributed {@code int} value between 0 (inclusive) and n
     *     (exclusive).
     * @throws IllegalArgumentException if n is not positive.
     */
    @Override
    public int nextInt(int n) {
        return randomGenerator.nextInt(n);
    }

    /**
     * Returns the next pseudorandom, uniformly distributed <code>long</code> value from this random
     * number generator's sequence. All 2<font size="-1"><sup>64</sup></font> possible {@code long}
     * values should be produced with (approximately) equal probability.
     *
     * @return the next pseudorandom, uniformly distributed <code>long</code> value from this random
     *     number generator's sequence
     */
    @Override
    public long nextLong() {
        return randomGenerator.nextLong();
    }

    /** {@inheritDoc} */
    public void setSeed(int seed) {
        if (randomGenerator != null) { // required to avoid NPE in constructor
            randomGenerator.setSeed(seed);
        }
    }

    /** {@inheritDoc} */
    public void setSeed(int[] seed) {
        if (randomGenerator != null) { // required to avoid NPE in constructor
            randomGenerator.setSeed(seed);
        }
    }

    /** {@inheritDoc} */
    @Override
    public void setSeed(long seed) {
        if (randomGenerator != null) { // required to avoid NPE in constructor
            randomGenerator.setSeed(seed);
        }
    }
}