JDKRandomGenerator.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.random;

  22. import java.io.Serializable;
  23. import java.util.Random;

  24. import org.hipparchus.exception.LocalizedCoreFormats;
  25. import org.hipparchus.exception.MathIllegalArgumentException;
  26. import org.hipparchus.util.MathUtils;

  27. /**
  28.  * A {@link RandomGenerator} adapter that delegates the random number
  29.  * generation to the standard {@link java.util.Random} class.
  30.  */
  31. public class JDKRandomGenerator extends IntRandomGenerator implements Serializable {

  32.     /** Serializable version identifier. */
  33.     private static final long serialVersionUID = 20151227L;

  34.     /** JDK's RNG. */
  35.     private final Random delegate;

  36.     /**
  37.      * Creates an instance with an arbitrary seed.
  38.      */
  39.     public JDKRandomGenerator() {
  40.         delegate = new Random();
  41.     }

  42.     /**
  43.      * Creates an instance with the given seed.
  44.      *
  45.      * @param seed Initial seed.
  46.      */
  47.     public JDKRandomGenerator(long seed) {
  48.         delegate = new Random(seed);
  49.     }

  50.     /**
  51.      * Creates an instance that wraps the given {@link Random} instance.
  52.      *
  53.      * @param random JDK {@link Random} instance that will generate the
  54.      * the random data.
  55.      * @throws MathIllegalArgumentException if random is null
  56.      */
  57.     public JDKRandomGenerator(Random random) {
  58.         MathUtils.checkNotNull(random);
  59.         delegate = random;
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public void setSeed(int seed) {
  64.         delegate.setSeed(seed);
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public void setSeed(long seed) {
  69.         delegate.setSeed(seed);
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public void setSeed(int[] seed) {
  74.         delegate.setSeed(convertToLong(seed));
  75.     }

  76.     /** {@inheritDoc} */
  77.     @Override
  78.     public void nextBytes(byte[] bytes) {
  79.         delegate.nextBytes(bytes);
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public int nextInt() {
  84.         return delegate.nextInt();
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public long nextLong() {
  89.         return delegate.nextLong();
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public boolean nextBoolean() {
  94.         return delegate.nextBoolean();
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public float nextFloat() {
  99.         return delegate.nextFloat();
  100.     }

  101.     /** {@inheritDoc} */
  102.     @Override
  103.     public double nextDouble() {
  104.         return delegate.nextDouble();
  105.     }

  106.     /** {@inheritDoc} */
  107.     @Override
  108.     public double nextGaussian() {
  109.         return delegate.nextGaussian();
  110.     }

  111.     /** {@inheritDoc} */
  112.     @Override
  113.     public int nextInt(int n) {
  114.         try {
  115.             return delegate.nextInt(n);
  116.         } catch (IllegalArgumentException e) {
  117.             throw new MathIllegalArgumentException(e, LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
  118.                                                    n, 0);
  119.         }
  120.     }

  121.     /**
  122.      * Converts seed from one representation to another.
  123.      *
  124.      * @param seed Original seed.
  125.      * @return the converted seed.
  126.      */
  127.     private static long convertToLong(int[] seed) {
  128.         // The following number is the largest prime that fits
  129.         // in 32 bits (i.e. 2^32 - 5).
  130.         final long prime = 4294967291l;

  131.         long combined = 0l;
  132.         for (int s : seed) {
  133.             combined = combined * prime + s;
  134.         }

  135.         return combined;
  136.     }

  137. }