JDKRandomWrapper.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.  *      http://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. package org.apache.commons.rng.simple;

  18. import org.apache.commons.rng.UniformRandomProvider;

  19. import java.util.Random;

  20. /**
  21.  * Wraps a {@link Random} instance to implement {@link UniformRandomProvider}. All methods from
  22.  * the {@code Random} that match those in {@code UniformRandomProvider} are used directly.
  23.  *
  24.  * <p>This class can be used to wrap an instance of
  25.  * {@link java.security.SecureRandom SecureRandom}. The {@code SecureRandom} class provides
  26.  * cryptographic random number generation. The features available depend on the Java version
  27.  * and platform. Consult the Java documentation for more details.</p>
  28.  *
  29.  * <p>Note: Use of {@code java.util.Random} is <em>not</em> recommended for applications.
  30.  * There are many other pseudo-random number generators that are statistically superior and often
  31.  * faster (see {@link RandomSource}).</p>
  32.  *
  33.  * @see java.security.SecureRandom
  34.  * @see RandomSource
  35.  * @since 1.3
  36.  */
  37. public final class JDKRandomWrapper implements UniformRandomProvider {
  38.     /** The JDK Random instance. */
  39.     private final Random rng;

  40.     /**
  41.      * Create a wrapper around a Random instance.
  42.      *
  43.      * @param rng JDK {@link Random} instance to which the random number
  44.      * generation is delegated.
  45.      */
  46.     public JDKRandomWrapper(Random rng) {
  47.         this.rng = rng;
  48.     }

  49.     /** {@inheritDoc} */
  50.     @Override
  51.     public void nextBytes(byte[] bytes) {
  52.         rng.nextBytes(bytes);
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public void nextBytes(byte[] bytes,
  57.                           int start,
  58.                           int len) {
  59.         final byte[] reduced = new byte[len];
  60.         rng.nextBytes(reduced);
  61.         System.arraycopy(reduced, 0, bytes, start, len);
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public int nextInt() {
  66.         return rng.nextInt();
  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public int nextInt(int n) {
  71.         return rng.nextInt(n);
  72.     }

  73.     /** {@inheritDoc} */
  74.     @Override
  75.     public long nextLong() {
  76.         return rng.nextLong();
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public long nextLong(long n) {
  81.         // Code copied from "o.a.c.rng.core.BaseProvider".
  82.         if (n <= 0) {
  83.             throw new IllegalArgumentException("Must be strictly positive: " + n);
  84.         }

  85.         long bits;
  86.         long val;
  87.         do {
  88.             bits = nextLong() >>> 1;
  89.             val  = bits % n;
  90.         } while (bits - val + (n - 1) < 0);

  91.         return val;
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public boolean nextBoolean() {
  96.         return rng.nextBoolean();
  97.     }

  98.     /** {@inheritDoc} */
  99.     @Override
  100.     public float nextFloat() {
  101.         return rng.nextFloat();
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     public double nextDouble() {
  106.         return rng.nextDouble();
  107.     }
  108. }