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