001: /*
002: * Copyright 2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.random;
017:
018: import java.util.Random;
019:
020: /**
021: * Extension of <code>java.util.Random</code> wrapping a
022: * {@link RandomGenerator}.
023: *
024: * @since 1.1
025: * @version $Revision:$ $Date$
026: */
027: public class RandomAdaptor extends Random implements RandomGenerator {
028:
029: /** Wrapped randomGenerator instance */
030: private RandomGenerator randomGenerator = null;
031:
032: /**
033: * Prevent instantiation without a generator argument
034: */
035: private RandomAdaptor() {
036: }
037:
038: /**
039: * Construct a RandomAdaptor wrapping the supplied RandomGenerator.
040: *
041: * @param randomGenerator the wrapped generator
042: */
043: public RandomAdaptor(RandomGenerator randomGenerator) {
044: this .randomGenerator = randomGenerator;
045: }
046:
047: /**
048: * Factory method to create a <code>Random</code> using the supplied
049: * <code>RandomGenerator</code>.
050: *
051: * @param randomGenerator wrapped RandomGenerator instance
052: * @return a Random instance wrapping the RandomGenerator
053: */
054: public static Random createAdaptor(RandomGenerator randomGenerator) {
055: return new RandomAdaptor(randomGenerator);
056: }
057:
058: /**
059: * Returns the next pseudorandom, uniformly distributed
060: * <code>boolean</code> value from this random number generator's
061: * sequence.
062: *
063: * @return the next pseudorandom, uniformly distributed
064: * <code>boolean</code> value from this random number generator's
065: * sequence
066: */
067: public boolean nextBoolean() {
068: return randomGenerator.nextBoolean();
069: }
070:
071: /**
072: * Generates random bytes and places them into a user-supplied
073: * byte array. The number of random bytes produced is equal to
074: * the length of the byte array.
075: *
076: * @param bytes the non-null byte array in which to put the
077: * random bytes
078: */
079: public void nextBytes(byte[] bytes) {
080: randomGenerator.nextBytes(bytes);
081: }
082:
083: /**
084: * Returns the next pseudorandom, uniformly distributed
085: * <code>double</code> value between <code>0.0</code> and
086: * <code>1.0</code> from this random number generator's sequence.
087: *
088: * @return the next pseudorandom, uniformly distributed
089: * <code>double</code> value between <code>0.0</code> and
090: * <code>1.0</code> from this random number generator's sequence
091: */
092: public double nextDouble() {
093: return randomGenerator.nextDouble();
094: }
095:
096: /**
097: * Returns the next pseudorandom, uniformly distributed <code>float</code>
098: * value between <code>0.0</code> and <code>1.0</code> from this random
099: * number generator's sequence.
100: *
101: * @return the next pseudorandom, uniformly distributed <code>float</code>
102: * value between <code>0.0</code> and <code>1.0</code> from this
103: * random number generator's sequence
104: */
105: public float nextFloat() {
106: return randomGenerator.nextFloat();
107: }
108:
109: /**
110: * Returns the next pseudorandom, Gaussian ("normally") distributed
111: * <code>double</code> value with mean <code>0.0</code> and standard
112: * deviation <code>1.0</code> from this random number generator's sequence.
113: *
114: * @return the next pseudorandom, Gaussian ("normally") distributed
115: * <code>double</code> value with mean <code>0.0</code> and
116: * standard deviation <code>1.0</code> from this random number
117: * generator's sequence
118: */
119: public double nextGaussian() {
120: return randomGenerator.nextGaussian();
121: }
122:
123: /**
124: * Returns the next pseudorandom, uniformly distributed <code>int</code>
125: * value from this random number generator's sequence.
126: * All 2<font size="-1"><sup>32</sup></font> possible <tt>int</tt> values
127: * should be produced with (approximately) equal probability.
128: *
129: * @return the next pseudorandom, uniformly distributed <code>int</code>
130: * value from this random number generator's sequence
131: */
132: public int nextInt() {
133: return randomGenerator.nextInt();
134: }
135:
136: /**
137: * Returns a pseudorandom, uniformly distributed <tt>int</tt> value
138: * between 0 (inclusive) and the specified value (exclusive), drawn from
139: * this random number generator's sequence.
140: *
141: * @param n the bound on the random number to be returned. Must be
142: * positive.
143: * @return a pseudorandom, uniformly distributed <tt>int</tt>
144: * value between 0 (inclusive) and n (exclusive).
145: * @throws IllegalArgumentException if n is not positive.
146: */
147: public int nextInt(int n) {
148: return randomGenerator.nextInt(n);
149: }
150:
151: /**
152: * Returns the next pseudorandom, uniformly distributed <code>long</code>
153: * value from this random number generator's sequence. All
154: * 2<font size="-1"><sup>64</sup></font> possible <tt>long</tt> values
155: * should be produced with (approximately) equal probability.
156: *
157: * @return the next pseudorandom, uniformly distributed <code>long</code>
158: *value from this random number generator's sequence
159: */
160: public long nextLong() {
161: return randomGenerator.nextLong();
162: }
163:
164: /**
165: * Sets the seed of the underyling random number generator using a
166: * <code>long</code> seed. Sequences of values generated starting with the
167: * same seeds should be identical.
168: *
169: * @param seed the seed value
170: */
171: public void setSeed(long seed) {
172: if (randomGenerator != null) { // required to avoid NPE in constructor
173: randomGenerator.setSeed(seed);
174: }
175: }
176: }
|