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     */
017    package org.apache.commons.math3.random;
018    
019    import java.util.Random;
020    
021    /**
022     * Extension of <code>java.util.Random</code> wrapping a
023     * {@link RandomGenerator}.
024     *
025     * @since 1.1
026     * @version $Id: RandomAdaptor.java 1416643 2012-12-03 19:37:14Z tn $
027     */
028    public class RandomAdaptor extends Random implements RandomGenerator {
029    
030        /** Serializable version identifier. */
031        private static final long serialVersionUID = 2306581345647615033L;
032    
033        /** Wrapped randomGenerator instance */
034        private final RandomGenerator randomGenerator;
035    
036        /**
037         * Prevent instantiation without a generator argument
038         */
039        @SuppressWarnings("unused")
040        private RandomAdaptor() { randomGenerator = null; }
041    
042        /**
043         * Construct a RandomAdaptor wrapping the supplied RandomGenerator.
044         *
045         * @param randomGenerator  the wrapped generator
046         */
047        public RandomAdaptor(RandomGenerator randomGenerator) {
048            this.randomGenerator = randomGenerator;
049        }
050    
051        /**
052         * Factory method to create a <code>Random</code> using the supplied
053         * <code>RandomGenerator</code>.
054         *
055         * @param randomGenerator  wrapped RandomGenerator instance
056         * @return a Random instance wrapping the RandomGenerator
057         */
058        public static Random createAdaptor(RandomGenerator randomGenerator) {
059            return new RandomAdaptor(randomGenerator);
060        }
061    
062        /**
063         * Returns the next pseudorandom, uniformly distributed
064         * <code>boolean</code> value from this random number generator's
065         * sequence.
066         *
067         * @return  the next pseudorandom, uniformly distributed
068         * <code>boolean</code> value from this random number generator's
069         * sequence
070         */
071        @Override
072        public boolean nextBoolean() {
073            return randomGenerator.nextBoolean();
074        }
075    
076         /**
077         * Generates random bytes and places them into a user-supplied
078         * byte array.  The number of random bytes produced is equal to
079         * the length of the byte array.
080         *
081         * @param bytes the non-null byte array in which to put the
082         * random bytes
083         */
084        @Override
085        public void nextBytes(byte[] bytes) {
086            randomGenerator.nextBytes(bytes);
087        }
088    
089         /**
090         * Returns the next pseudorandom, uniformly distributed
091         * <code>double</code> value between <code>0.0</code> and
092         * <code>1.0</code> from this random number generator's sequence.
093         *
094         * @return  the next pseudorandom, uniformly distributed
095         *  <code>double</code> value between <code>0.0</code> and
096         *  <code>1.0</code> from this random number generator's sequence
097         */
098        @Override
099        public double nextDouble() {
100            return randomGenerator.nextDouble();
101        }
102    
103        /**
104         * Returns the next pseudorandom, uniformly distributed <code>float</code>
105         * value between <code>0.0</code> and <code>1.0</code> from this random
106         * number generator's sequence.
107         *
108         * @return  the next pseudorandom, uniformly distributed <code>float</code>
109         * value between <code>0.0</code> and <code>1.0</code> from this
110         * random number generator's sequence
111         */
112        @Override
113        public float nextFloat() {
114            return randomGenerator.nextFloat();
115        }
116    
117        /**
118         * Returns the next pseudorandom, Gaussian ("normally") distributed
119         * <code>double</code> value with mean <code>0.0</code> and standard
120         * deviation <code>1.0</code> from this random number generator's sequence.
121         *
122         * @return  the next pseudorandom, Gaussian ("normally") distributed
123         * <code>double</code> value with mean <code>0.0</code> and
124         * standard deviation <code>1.0</code> from this random number
125         *  generator's sequence
126         */
127        @Override
128        public double nextGaussian() {
129            return randomGenerator.nextGaussian();
130        }
131    
132         /**
133         * Returns the next pseudorandom, uniformly distributed <code>int</code>
134         * value from this random number generator's sequence.
135         * All 2<font size="-1"><sup>32</sup></font> possible <tt>int</tt> values
136         * should be produced with  (approximately) equal probability.
137         *
138         * @return the next pseudorandom, uniformly distributed <code>int</code>
139         *  value from this random number generator's sequence
140         */
141        @Override
142        public int nextInt() {
143            return randomGenerator.nextInt();
144        }
145    
146        /**
147         * Returns a pseudorandom, uniformly distributed <tt>int</tt> value
148         * between 0 (inclusive) and the specified value (exclusive), drawn from
149         * this random number generator's sequence.
150         *
151         * @param n the bound on the random number to be returned.  Must be
152         * positive.
153         * @return  a pseudorandom, uniformly distributed <tt>int</tt>
154         * value between 0 (inclusive) and n (exclusive).
155         * @throws IllegalArgumentException  if n is not positive.
156         */
157        @Override
158        public int nextInt(int n) {
159            return randomGenerator.nextInt(n);
160        }
161    
162        /**
163         * Returns the next pseudorandom, uniformly distributed <code>long</code>
164         * value from this random number generator's sequence.  All
165         * 2<font size="-1"><sup>64</sup></font> possible <tt>long</tt> values
166         * should be produced with (approximately) equal probability.
167         *
168         * @return  the next pseudorandom, uniformly distributed <code>long</code>
169         *value from this random number generator's sequence
170         */
171        @Override
172        public long nextLong() {
173            return randomGenerator.nextLong();
174        }
175    
176        /** {@inheritDoc} */
177        public void setSeed(int seed) {
178            if (randomGenerator != null) {  // required to avoid NPE in constructor
179                randomGenerator.setSeed(seed);
180            }
181        }
182    
183        /** {@inheritDoc} */
184        public void setSeed(int[] seed) {
185            if (randomGenerator != null) {  // required to avoid NPE in constructor
186                randomGenerator.setSeed(seed);
187            }
188        }
189    
190        /** {@inheritDoc} */
191        @Override
192        public void setSeed(long seed) {
193            if (randomGenerator != null) {  // required to avoid NPE in constructor
194                randomGenerator.setSeed(seed);
195            }
196        }
197    
198    }