View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.random;
23  
24  import java.util.Random;
25  
26  import org.hipparchus.util.MathUtils;
27  
28  /**
29   * Extension of {@link java.util.Random} wrapping a
30   * {@link RandomGenerator}.
31   */
32  public class RandomAdaptor extends Random implements RandomGenerator {
33  
34      /** Serializable version identifier. */
35      private static final long serialVersionUID = 20160529L;
36  
37      /** Wrapped randomGenerator instance */
38      private final RandomGenerator randomGenerator;
39  
40      /**
41       * Construct a RandomAdaptor wrapping the supplied RandomGenerator.
42       *
43       * @param randomGenerator  the wrapped generator
44       * @throws org.hipparchus.exception.NullArgumentException if randomGenerator is null
45       */
46      public RandomAdaptor(RandomGenerator randomGenerator) {
47          MathUtils.checkNotNull(randomGenerator);
48          this.randomGenerator = randomGenerator;
49      }
50  
51      /**
52       * Factory method to create a <code>Random</code> using the supplied
53       * <code>RandomGenerator</code>.
54       *
55       * @param randomGenerator  wrapped RandomGenerator instance
56       * @return a Random instance wrapping the RandomGenerator
57       */
58      public static Random of(RandomGenerator randomGenerator) {
59          return new RandomAdaptor(randomGenerator);
60      }
61  
62      /**
63       * Returns the next pseudorandom, uniformly distributed
64       * <code>boolean</code> value from this random number generator's
65       * sequence.
66       *
67       * @return  the next pseudorandom, uniformly distributed
68       * <code>boolean</code> value from this random number generator's
69       * sequence
70       */
71      @Override
72      public boolean nextBoolean() {
73          return randomGenerator.nextBoolean();
74      }
75  
76      /**
77       * Generates random bytes and places them into a user-supplied
78       * byte array.  The number of random bytes produced is equal to
79       * the length of the byte array.
80       *
81       * @param bytes the non-null byte array in which to put the
82       * random bytes
83       */
84      @Override
85      public void nextBytes(byte[] bytes) {
86          randomGenerator.nextBytes(bytes);
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public void nextBytes(byte[] bytes, int offset, int len) {
92          randomGenerator.nextBytes(bytes, offset, len);
93      }
94  
95      /**
96       * Returns the next pseudorandom, uniformly distributed
97       * <code>double</code> value between <code>0.0</code> and
98       * <code>1.0</code> from this random number generator's sequence.
99       *
100      * @return  the next pseudorandom, uniformly distributed
101      *  <code>double</code> value between <code>0.0</code> and
102      *  <code>1.0</code> from this random number generator's sequence
103      */
104     @Override
105     public double nextDouble() {
106         return randomGenerator.nextDouble();
107     }
108 
109     /**
110      * Returns the next pseudorandom, uniformly distributed <code>float</code>
111      * value between <code>0.0</code> and <code>1.0</code> from this random
112      * number generator's sequence.
113      *
114      * @return  the next pseudorandom, uniformly distributed <code>float</code>
115      * value between <code>0.0</code> and <code>1.0</code> from this
116      * random number generator's sequence
117      */
118     @Override
119     public float nextFloat() {
120         return randomGenerator.nextFloat();
121     }
122 
123     /**
124      * Returns the next pseudorandom, Gaussian ("normally") distributed
125      * <code>double</code> value with mean <code>0.0</code> and standard
126      * deviation <code>1.0</code> from this random number generator's sequence.
127      *
128      * @return  the next pseudorandom, Gaussian ("normally") distributed
129      * <code>double</code> value with mean <code>0.0</code> and
130      * standard deviation <code>1.0</code> from this random number
131      *  generator's sequence
132      */
133     @Override
134     public double nextGaussian() {
135         return randomGenerator.nextGaussian();
136     }
137 
138      /**
139      * Returns the next pseudorandom, uniformly distributed <code>int</code>
140      * value from this random number generator's sequence.
141      * All 2<sup>32</sup> possible {@code int} values
142      * should be produced with  (approximately) equal probability.
143      *
144      * @return the next pseudorandom, uniformly distributed <code>int</code>
145      *  value from this random number generator's sequence
146      */
147     @Override
148     public int nextInt() {
149         return randomGenerator.nextInt();
150     }
151 
152     /**
153      * Returns a pseudorandom, uniformly distributed {@code int} value
154      * between 0 (inclusive) and the specified value (exclusive), drawn from
155      * this random number generator's sequence.
156      *
157      * @param n the bound on the random number to be returned.  Must be
158      * positive.
159      * @return  a pseudorandom, uniformly distributed {@code int}
160      * value between 0 (inclusive) and n (exclusive).
161      * @throws IllegalArgumentException  if n is not positive.
162      */
163     @Override
164     public int nextInt(int n) {
165         return randomGenerator.nextInt(n);
166     }
167 
168     /**
169      * Returns the next pseudorandom, uniformly distributed <code>long</code>
170      * value from this random number generator's sequence.  All
171      * 2<sup>64</sup> possible {@code long} values
172      * should be produced with (approximately) equal probability.
173      *
174      * @return  the next pseudorandom, uniformly distributed <code>long</code>
175      * value from this random number generator's sequence
176      */
177     @Override
178     public long nextLong() {
179         return randomGenerator.nextLong();
180     }
181 
182     /** {@inheritDoc} */
183     @Override
184     public long nextLong(long n) {
185         return randomGenerator.nextLong(n);
186     }
187 
188     /** {@inheritDoc} */
189     @Override
190     public void setSeed(int seed) {
191         if (randomGenerator != null) {  // required to avoid NPE in constructor
192             randomGenerator.setSeed(seed);
193         }
194     }
195 
196     /** {@inheritDoc} */
197     @Override
198     public void setSeed(int[] seed) {
199         if (randomGenerator != null) {  // required to avoid NPE in constructor
200             randomGenerator.setSeed(seed);
201         }
202     }
203 
204     /** {@inheritDoc} */
205     @Override
206     public void setSeed(long seed) {
207         if (randomGenerator != null) {  // required to avoid NPE in constructor
208             randomGenerator.setSeed(seed);
209         }
210     }
211 
212 }