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.io.Serializable;
25  import java.util.Random;
26  
27  import org.hipparchus.exception.LocalizedCoreFormats;
28  import org.hipparchus.exception.MathIllegalArgumentException;
29  import org.hipparchus.util.MathUtils;
30  
31  /**
32   * A {@link RandomGenerator} adapter that delegates the random number
33   * generation to the standard {@link java.util.Random} class.
34   */
35  public class JDKRandomGenerator extends IntRandomGenerator implements Serializable {
36  
37      /** Serializable version identifier. */
38      private static final long serialVersionUID = 20151227L;
39  
40      /** JDK's RNG. */
41      private final Random delegate;
42  
43      /**
44       * Creates an instance with an arbitrary seed.
45       */
46      public JDKRandomGenerator() {
47          delegate = new Random();
48      }
49  
50      /**
51       * Creates an instance with the given seed.
52       *
53       * @param seed Initial seed.
54       */
55      public JDKRandomGenerator(long seed) {
56          delegate = new Random(seed);
57      }
58  
59      /**
60       * Creates an instance that wraps the given {@link Random} instance.
61       *
62       * @param random JDK {@link Random} instance that will generate the
63       * the random data.
64       * @throws MathIllegalArgumentException if random is null
65       */
66      public JDKRandomGenerator(Random random) {
67          MathUtils.checkNotNull(random);
68          delegate = random;
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public void setSeed(int seed) {
74          delegate.setSeed(seed);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public void setSeed(long seed) {
80          delegate.setSeed(seed);
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public void setSeed(int[] seed) {
86          delegate.setSeed(convertToLong(seed));
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public void nextBytes(byte[] bytes) {
92          delegate.nextBytes(bytes);
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public int nextInt() {
98          return delegate.nextInt();
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public long nextLong() {
104         return delegate.nextLong();
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public boolean nextBoolean() {
110         return delegate.nextBoolean();
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public float nextFloat() {
116         return delegate.nextFloat();
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public double nextDouble() {
122         return delegate.nextDouble();
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public double nextGaussian() {
128         return delegate.nextGaussian();
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public int nextInt(int n) {
134         try {
135             return delegate.nextInt(n);
136         } catch (IllegalArgumentException e) {
137             throw new MathIllegalArgumentException(e, LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
138                                                    n, 0);
139         }
140     }
141 
142     /**
143      * Converts seed from one representation to another.
144      *
145      * @param seed Original seed.
146      * @return the converted seed.
147      */
148     private static long convertToLong(int[] seed) {
149         // The following number is the largest prime that fits
150         // in 32 bits (i.e. 2^32 - 5).
151         final long prime = 4294967291l;
152 
153         long combined = 0l;
154         for (int s : seed) {
155             combined = combined * prime + s;
156         }
157 
158         return combined;
159     }
160 
161 }