RandomAdaptor.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.util.Random;

  23. import org.hipparchus.util.MathUtils;

  24. /**
  25.  * Extension of {@link java.util.Random} wrapping a
  26.  * {@link RandomGenerator}.
  27.  */
  28. public class RandomAdaptor extends Random implements RandomGenerator {

  29.     /** Serializable version identifier. */
  30.     private static final long serialVersionUID = 20160529L;

  31.     /** Wrapped randomGenerator instance */
  32.     private final RandomGenerator randomGenerator;

  33.     /**
  34.      * Construct a RandomAdaptor wrapping the supplied RandomGenerator.
  35.      *
  36.      * @param randomGenerator  the wrapped generator
  37.      * @throws org.hipparchus.exception.NullArgumentException if randomGenerator is null
  38.      */
  39.     public RandomAdaptor(RandomGenerator randomGenerator) {
  40.         MathUtils.checkNotNull(randomGenerator);
  41.         this.randomGenerator = randomGenerator;
  42.     }

  43.     /**
  44.      * Factory method to create a <code>Random</code> using the supplied
  45.      * <code>RandomGenerator</code>.
  46.      *
  47.      * @param randomGenerator  wrapped RandomGenerator instance
  48.      * @return a Random instance wrapping the RandomGenerator
  49.      */
  50.     public static Random of(RandomGenerator randomGenerator) {
  51.         return new RandomAdaptor(randomGenerator);
  52.     }

  53.     /**
  54.      * Returns the next pseudorandom, uniformly distributed
  55.      * <code>boolean</code> value from this random number generator's
  56.      * sequence.
  57.      *
  58.      * @return  the next pseudorandom, uniformly distributed
  59.      * <code>boolean</code> value from this random number generator's
  60.      * sequence
  61.      */
  62.     @Override
  63.     public boolean nextBoolean() {
  64.         return randomGenerator.nextBoolean();
  65.     }

  66.     /**
  67.      * Generates random bytes and places them into a user-supplied
  68.      * byte array.  The number of random bytes produced is equal to
  69.      * the length of the byte array.
  70.      *
  71.      * @param bytes the non-null byte array in which to put the
  72.      * random bytes
  73.      */
  74.     @Override
  75.     public void nextBytes(byte[] bytes) {
  76.         randomGenerator.nextBytes(bytes);
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public void nextBytes(byte[] bytes, int offset, int len) {
  81.         randomGenerator.nextBytes(bytes, offset, len);
  82.     }

  83.     /**
  84.      * Returns the next pseudorandom, uniformly distributed
  85.      * <code>double</code> value between <code>0.0</code> and
  86.      * <code>1.0</code> from this random number generator's sequence.
  87.      *
  88.      * @return  the next pseudorandom, uniformly distributed
  89.      *  <code>double</code> value between <code>0.0</code> and
  90.      *  <code>1.0</code> from this random number generator's sequence
  91.      */
  92.     @Override
  93.     public double nextDouble() {
  94.         return randomGenerator.nextDouble();
  95.     }

  96.     /**
  97.      * Returns the next pseudorandom, uniformly distributed <code>float</code>
  98.      * value between <code>0.0</code> and <code>1.0</code> from this random
  99.      * number generator's sequence.
  100.      *
  101.      * @return  the next pseudorandom, uniformly distributed <code>float</code>
  102.      * value between <code>0.0</code> and <code>1.0</code> from this
  103.      * random number generator's sequence
  104.      */
  105.     @Override
  106.     public float nextFloat() {
  107.         return randomGenerator.nextFloat();
  108.     }

  109.     /**
  110.      * Returns the next pseudorandom, Gaussian ("normally") distributed
  111.      * <code>double</code> value with mean <code>0.0</code> and standard
  112.      * deviation <code>1.0</code> from this random number generator's sequence.
  113.      *
  114.      * @return  the next pseudorandom, Gaussian ("normally") distributed
  115.      * <code>double</code> value with mean <code>0.0</code> and
  116.      * standard deviation <code>1.0</code> from this random number
  117.      *  generator's sequence
  118.      */
  119.     @Override
  120.     public double nextGaussian() {
  121.         return randomGenerator.nextGaussian();
  122.     }

  123.      /**
  124.      * Returns the next pseudorandom, uniformly distributed <code>int</code>
  125.      * value from this random number generator's sequence.
  126.      * All 2<sup>32</sup> possible {@code int} values
  127.      * should be produced with  (approximately) equal probability.
  128.      *
  129.      * @return the next pseudorandom, uniformly distributed <code>int</code>
  130.      *  value from this random number generator's sequence
  131.      */
  132.     @Override
  133.     public int nextInt() {
  134.         return randomGenerator.nextInt();
  135.     }

  136.     /**
  137.      * Returns a pseudorandom, uniformly distributed {@code int} value
  138.      * between 0 (inclusive) and the specified value (exclusive), drawn from
  139.      * this random number generator's sequence.
  140.      *
  141.      * @param n the bound on the random number to be returned.  Must be
  142.      * positive.
  143.      * @return  a pseudorandom, uniformly distributed {@code int}
  144.      * value between 0 (inclusive) and n (exclusive).
  145.      * @throws IllegalArgumentException  if n is not positive.
  146.      */
  147.     @Override
  148.     public int nextInt(int n) {
  149.         return randomGenerator.nextInt(n);
  150.     }

  151.     /**
  152.      * Returns the next pseudorandom, uniformly distributed <code>long</code>
  153.      * value from this random number generator's sequence.  All
  154.      * 2<sup>64</sup> possible {@code long} values
  155.      * should be produced with (approximately) equal probability.
  156.      *
  157.      * @return  the next pseudorandom, uniformly distributed <code>long</code>
  158.      * value from this random number generator's sequence
  159.      */
  160.     @Override
  161.     public long nextLong() {
  162.         return randomGenerator.nextLong();
  163.     }

  164.     /** {@inheritDoc} */
  165.     @Override
  166.     public long nextLong(long n) {
  167.         return randomGenerator.nextLong(n);
  168.     }

  169.     /** {@inheritDoc} */
  170.     @Override
  171.     public void setSeed(int seed) {
  172.         if (randomGenerator != null) {  // required to avoid NPE in constructor
  173.             randomGenerator.setSeed(seed);
  174.         }
  175.     }

  176.     /** {@inheritDoc} */
  177.     @Override
  178.     public void setSeed(int[] seed) {
  179.         if (randomGenerator != null) {  // required to avoid NPE in constructor
  180.             randomGenerator.setSeed(seed);
  181.         }
  182.     }

  183.     /** {@inheritDoc} */
  184.     @Override
  185.     public void setSeed(long seed) {
  186.         if (randomGenerator != null) {  // required to avoid NPE in constructor
  187.             randomGenerator.setSeed(seed);
  188.         }
  189.     }

  190. }