UniformRandomProvider.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;

  18. import java.util.stream.DoubleStream;
  19. import java.util.stream.IntStream;
  20. import java.util.stream.LongStream;

  21. /**
  22.  * Applies to generators of random number sequences that follow a uniform
  23.  * distribution.
  24.  *
  25.  * @since 1.0
  26.  */
  27. public interface UniformRandomProvider {
  28.     /**
  29.      * Generates {@code byte} values and places them into a user-supplied array.
  30.      *
  31.      * <p>The number of random bytes produced is equal to the length of the byte array.
  32.      *
  33.      * @param bytes Byte array in which to put the random bytes.
  34.      * Cannot be {@code null}.
  35.      */
  36.     default void nextBytes(byte[] bytes) {
  37.         UniformRandomProviderSupport.nextBytes(this, bytes, 0, bytes.length);
  38.     }

  39.     /**
  40.      * Generates {@code byte} values and places them into a user-supplied array.
  41.      *
  42.      * <p>The array is filled with bytes extracted from random integers.
  43.      * This implies that the number of random bytes generated may be larger than
  44.      * the length of the byte array.
  45.      *
  46.      * @param bytes Array in which to put the generated bytes.
  47.      * Cannot be {@code null}.
  48.      * @param start Index at which to start inserting the generated bytes.
  49.      * @param len Number of bytes to insert.
  50.      * @throws IndexOutOfBoundsException if {@code start < 0} or
  51.      * {@code start >= bytes.length}.
  52.      * @throws IndexOutOfBoundsException if {@code len < 0} or
  53.      * {@code len > bytes.length - start}.
  54.      */
  55.     default void nextBytes(byte[] bytes, int start, int len) {
  56.         UniformRandomProviderSupport.validateFromIndexSize(start, len, bytes.length);
  57.         UniformRandomProviderSupport.nextBytes(this, bytes, start, len);
  58.     }

  59.     /**
  60.      * Generates an {@code int} value.
  61.      *
  62.      * @return the next random value.
  63.      */
  64.     default int nextInt() {
  65.         return (int) (nextLong() >>> 32);
  66.     }

  67.     /**
  68.      * Generates an {@code int} value between 0 (inclusive) and the
  69.      * specified value (exclusive).
  70.      *
  71.      * @param n Bound on the random number to be returned.  Must be positive.
  72.      * @return a random {@code int} value between 0 (inclusive) and {@code n}
  73.      * (exclusive).
  74.      * @throws IllegalArgumentException if {@code n} is not above zero.
  75.      */
  76.     default int nextInt(int n) {
  77.         UniformRandomProviderSupport.validateUpperBound(n);
  78.         return UniformRandomProviderSupport.nextInt(this, n);
  79.     }

  80.     /**
  81.      * Generates an {@code int} value between the specified {@code origin} (inclusive) and
  82.      * the specified {@code bound} (exclusive).
  83.      *
  84.      * @param origin Lower bound on the random number to be returned.
  85.      * @param bound Upper bound (exclusive) on the random number to be returned.
  86.      * @return a random {@code int} value between {@code origin} (inclusive) and
  87.      * {@code bound} (exclusive).
  88.      * @throws IllegalArgumentException if {@code origin} is greater than or equal to
  89.      * {@code bound}.
  90.      * @since 1.5
  91.      */
  92.     default int nextInt(int origin, int bound) {
  93.         UniformRandomProviderSupport.validateRange(origin, bound);
  94.         return UniformRandomProviderSupport.nextInt(this, origin, bound);
  95.     }

  96.     /**
  97.      * Generates a {@code long} value.
  98.      *
  99.      * @return the next random value.
  100.      */
  101.     long nextLong();

  102.     /**
  103.      * Generates a {@code long} value between 0 (inclusive) and the specified
  104.      * value (exclusive).
  105.      *
  106.      * @param n Bound on the random number to be returned.  Must be positive.
  107.      * @return a random {@code long} value between 0 (inclusive) and {@code n}
  108.      * (exclusive).
  109.      * @throws IllegalArgumentException if {@code n} is not greater than 0.
  110.      */
  111.     default long nextLong(long n) {
  112.         UniformRandomProviderSupport.validateUpperBound(n);
  113.         return UniformRandomProviderSupport.nextLong(this, n);
  114.     }

  115.     /**
  116.      * Generates a {@code long} value between the specified {@code origin} (inclusive) and
  117.      * the specified {@code bound} (exclusive).
  118.      *
  119.      * @param origin Lower bound on the random number to be returned.
  120.      * @param bound Upper bound (exclusive) on the random number to be returned.
  121.      * @return a random {@code long} value between {@code origin} (inclusive) and
  122.      * {@code bound} (exclusive).
  123.      * @throws IllegalArgumentException if {@code origin} is greater than or equal to
  124.      * {@code bound}.
  125.      * @since 1.5
  126.      */
  127.     default long nextLong(long origin, long bound) {
  128.         UniformRandomProviderSupport.validateRange(origin, bound);
  129.         return UniformRandomProviderSupport.nextLong(this, origin, bound);
  130.     }

  131.     /**
  132.      * Generates a {@code boolean} value.
  133.      *
  134.      * @return the next random value.
  135.      */
  136.     default boolean nextBoolean() {
  137.         return nextInt() < 0;
  138.     }

  139.     /**
  140.      * Generates a {@code float} value between 0 (inclusive) and 1 (exclusive).
  141.      *
  142.      * @return the next random value between 0 (inclusive) and 1 (exclusive).
  143.      */
  144.     default float nextFloat() {
  145.         return (nextInt() >>> 8) * 0x1.0p-24f;
  146.     }

  147.     /**
  148.      * Generates a {@code float} value between 0 (inclusive) and the
  149.      * specified {@code bound} (exclusive).
  150.      *
  151.      * @param bound Upper bound (exclusive) on the random number to be returned.
  152.      * @return a random {@code float} value between 0 (inclusive) and {@code bound}
  153.      * (exclusive).
  154.      * @throws IllegalArgumentException if {@code bound} is not both finite and greater than 0.
  155.      * @since 1.5
  156.      */
  157.     default float nextFloat(float bound) {
  158.         UniformRandomProviderSupport.validateUpperBound(bound);
  159.         return UniformRandomProviderSupport.nextFloat(this, bound);
  160.     }

  161.     /**
  162.      * Generates a {@code float} value between the specified {@code origin} (inclusive)
  163.      * and the specified {@code bound} (exclusive).
  164.      *
  165.      * @param origin Lower bound on the random number to be returned.
  166.      * @param bound Upper bound (exclusive) on the random number to be returned.
  167.      * @return a random {@code float} value between {@code origin} (inclusive) and
  168.      * {@code bound} (exclusive).
  169.      * @throws IllegalArgumentException if {@code origin} is not finite, or {@code bound}
  170.      * is not finite, or {@code origin} is greater than or equal to {@code bound}.
  171.      * @since 1.5
  172.      */
  173.     default float nextFloat(float origin, float bound) {
  174.         UniformRandomProviderSupport.validateRange(origin, bound);
  175.         return UniformRandomProviderSupport.nextFloat(this, origin, bound);
  176.     }

  177.     /**
  178.      * Generates a {@code double} value between 0 (inclusive) and 1 (exclusive).
  179.      *
  180.      * @return the next random value between 0 (inclusive) and 1 (exclusive).
  181.      */
  182.     default double nextDouble() {
  183.         return (nextLong() >>> 11) * 0x1.0p-53;
  184.     }

  185.     /**
  186.      * Generates a {@code double} value between 0 (inclusive) and the
  187.      * specified {@code bound} (exclusive).
  188.      *
  189.      * @param bound Upper bound (exclusive) on the random number to be returned.
  190.      * @return a random {@code double} value between 0 (inclusive) and {@code bound}
  191.      * (exclusive).
  192.      * @throws IllegalArgumentException if {@code bound} is not both finite and greater than 0.
  193.      * @since 1.5
  194.      */
  195.     default double nextDouble(double bound) {
  196.         UniformRandomProviderSupport.validateUpperBound(bound);
  197.         return UniformRandomProviderSupport.nextDouble(this, bound);
  198.     }

  199.     /**
  200.      * Generates a {@code double} value between the specified {@code origin} (inclusive)
  201.      * and the specified {@code bound} (exclusive).
  202.      *
  203.      * @param origin Lower bound on the random number to be returned.
  204.      * @param bound Upper bound (exclusive) on the random number to be returned.
  205.      * @return a random {@code double} value between {@code origin} (inclusive) and
  206.      * {@code bound} (exclusive).
  207.      * @throws IllegalArgumentException if {@code origin} is not finite, or {@code bound}
  208.      * is not finite, or {@code origin} is greater than or equal to {@code bound}.
  209.      * @since 1.5
  210.      */
  211.     default double nextDouble(double origin, double bound) {
  212.         UniformRandomProviderSupport.validateRange(origin, bound);
  213.         return UniformRandomProviderSupport.nextDouble(this, origin, bound);
  214.     }

  215.     /**
  216.      * Returns an effectively unlimited stream of {@code int} values.
  217.      *
  218.      * @return a stream of random {@code int} values.
  219.      * @since 1.5
  220.      */
  221.     default IntStream ints() {
  222.         return IntStream.generate(this::nextInt).sequential();
  223.     }

  224.     /**
  225.      * Returns an effectively unlimited stream of {@code int} values between the specified
  226.      * {@code origin} (inclusive) and the specified {@code bound} (exclusive).
  227.      *
  228.      * @param origin Lower bound on the random number to be returned.
  229.      * @param bound Upper bound (exclusive) on the random number to be returned.
  230.      * @return a stream of random values between the specified {@code origin} (inclusive)
  231.      * and the specified {@code bound} (exclusive).
  232.      * @throws IllegalArgumentException if {@code origin} is greater than or equal to
  233.      * {@code bound}.
  234.      * @since 1.5
  235.      */
  236.     default IntStream ints(int origin, int bound) {
  237.         UniformRandomProviderSupport.validateRange(origin, bound);
  238.         return IntStream.generate(() -> nextInt(origin, bound)).sequential();
  239.     }

  240.     /**
  241.      * Returns a stream producing the given {@code streamSize} number of {@code int}
  242.      * values.
  243.      *
  244.      * @param streamSize Number of values to generate.
  245.      * @return a stream of random {@code int} values; the stream is limited to the given
  246.      * {@code streamSize}.
  247.      * @throws IllegalArgumentException if {@code streamSize} is negative.
  248.      * @since 1.5
  249.      */
  250.     default IntStream ints(long streamSize) {
  251.         UniformRandomProviderSupport.validateStreamSize(streamSize);
  252.         return ints().limit(streamSize);
  253.     }

  254.     /**
  255.      * Returns a stream producing the given {@code streamSize} number of {@code int}
  256.      * values between the specified {@code origin} (inclusive) and the specified
  257.      * {@code bound} (exclusive).
  258.      *
  259.      * @param streamSize Number of values to generate.
  260.      * @param origin Lower bound on the random number to be returned.
  261.      * @param bound Upper bound (exclusive) on the random number to be returned.
  262.      * @return a stream of random values between the specified {@code origin} (inclusive)
  263.      * and the specified {@code bound} (exclusive); the stream is limited to the given
  264.      * {@code streamSize}.
  265.      * @throws IllegalArgumentException if {@code streamSize} is negative, or if
  266.      * {@code origin} is greater than or equal to {@code bound}.
  267.      * @since 1.5
  268.      */
  269.     default IntStream ints(long streamSize, int origin, int bound) {
  270.         UniformRandomProviderSupport.validateStreamSize(streamSize);
  271.         UniformRandomProviderSupport.validateRange(origin, bound);
  272.         return ints(origin, bound).limit(streamSize);
  273.     }

  274.     /**
  275.      * Returns an effectively unlimited stream of {@code long} values.
  276.      *
  277.      * @return a stream of random {@code long} values.
  278.      * @since 1.5
  279.      */
  280.     default LongStream longs() {
  281.         return LongStream.generate(this::nextLong).sequential();
  282.     }

  283.     /**
  284.      * Returns an effectively unlimited stream of {@code long} values between the
  285.      * specified {@code origin} (inclusive) and the specified {@code bound} (exclusive).
  286.      *
  287.      * @param origin Lower bound on the random number to be returned.
  288.      * @param bound Upper bound (exclusive) on the random number to be returned.
  289.      * @return a stream of random values between the specified {@code origin} (inclusive)
  290.      * and the specified {@code bound} (exclusive).
  291.      * @throws IllegalArgumentException if {@code origin} is greater than or equal to
  292.      * {@code bound}.
  293.      * @since 1.5
  294.      */
  295.     default LongStream longs(long origin, long bound) {
  296.         UniformRandomProviderSupport.validateRange(origin, bound);
  297.         return LongStream.generate(() -> nextLong(origin, bound)).sequential();
  298.     }

  299.     /**
  300.      * Returns a stream producing the given {@code streamSize} number of {@code long}
  301.      * values.
  302.      *
  303.      * @param streamSize Number of values to generate.
  304.      * @return a stream of random {@code long} values; the stream is limited to the given
  305.      * {@code streamSize}.
  306.      * @throws IllegalArgumentException if {@code streamSize} is negative.
  307.      * @since 1.5
  308.      */
  309.     default LongStream longs(long streamSize) {
  310.         UniformRandomProviderSupport.validateStreamSize(streamSize);
  311.         return longs().limit(streamSize);
  312.     }

  313.     /**
  314.      * Returns a stream producing the given {@code streamSize} number of {@code long}
  315.      * values between the specified {@code origin} (inclusive) and the specified
  316.      * {@code bound} (exclusive).
  317.      *
  318.      * @param streamSize Number of values to generate.
  319.      * @param origin Lower bound on the random number to be returned.
  320.      * @param bound Upper bound (exclusive) on the random number to be returned.
  321.      * @return a stream of random values between the specified {@code origin} (inclusive)
  322.      * and the specified {@code bound} (exclusive); the stream is limited to the given
  323.      * {@code streamSize}.
  324.      * @throws IllegalArgumentException if {@code streamSize} is negative, or if
  325.      * {@code origin} is greater than or equal to {@code bound}.
  326.      * @since 1.5
  327.      */
  328.     default LongStream longs(long streamSize, long origin, long bound) {
  329.         UniformRandomProviderSupport.validateStreamSize(streamSize);
  330.         UniformRandomProviderSupport.validateRange(origin, bound);
  331.         return longs(origin, bound).limit(streamSize);
  332.     }

  333.     /**
  334.      * Returns an effectively unlimited stream of {@code double} values between 0
  335.      * (inclusive) and 1 (exclusive).
  336.      *
  337.      * @return a stream of random values between 0 (inclusive) and 1 (exclusive).
  338.      * @since 1.5
  339.      */
  340.     default DoubleStream doubles() {
  341.         return DoubleStream.generate(this::nextDouble).sequential();
  342.     }

  343.     /**
  344.      * Returns an effectively unlimited stream of {@code double} values between the
  345.      * specified {@code origin} (inclusive) and the specified {@code bound} (exclusive).
  346.      *
  347.      * @param origin Lower bound on the random number to be returned.
  348.      * @param bound Upper bound (exclusive) on the random number to be returned.
  349.      * @return a stream of random values between the specified {@code origin} (inclusive)
  350.      * and the specified {@code bound} (exclusive).
  351.      * @throws IllegalArgumentException if {@code origin} is not finite, or {@code bound}
  352.      * is not finite, or {@code origin} is greater than or equal to {@code bound}.
  353.      * @since 1.5
  354.      */
  355.     default DoubleStream doubles(double origin, double bound) {
  356.         UniformRandomProviderSupport.validateRange(origin, bound);
  357.         return DoubleStream.generate(() -> nextDouble(origin, bound)).sequential();
  358.     }

  359.     /**
  360.      * Returns a stream producing the given {@code streamSize} number of {@code double}
  361.      * values between 0 (inclusive) and 1 (exclusive).
  362.      *
  363.      * @param streamSize Number of values to generate.
  364.      * @return a stream of random values between 0 (inclusive) and 1 (exclusive);
  365.      * the stream is limited to the given {@code streamSize}.
  366.      * @throws IllegalArgumentException if {@code streamSize} is negative.
  367.      * @since 1.5
  368.      */
  369.     default DoubleStream doubles(long streamSize) {
  370.         UniformRandomProviderSupport.validateStreamSize(streamSize);
  371.         return doubles().limit(streamSize);
  372.     }

  373.     /**
  374.      * Returns a stream producing the given {@code streamSize} number of {@code double}
  375.      * values between the specified {@code origin} (inclusive) and the specified
  376.      * {@code bound} (exclusive).
  377.      *
  378.      * @param streamSize Number of values to generate.
  379.      * @param origin Lower bound on the random number to be returned.
  380.      * @param bound Upper bound (exclusive) on the random number to be returned.
  381.      * @return a stream of random values between the specified {@code origin} (inclusive)
  382.      * and the specified {@code bound} (exclusive); the stream is limited to the given
  383.      * {@code streamSize}.
  384.      * @throws IllegalArgumentException if {@code streamSize} is negative, or if
  385.      * {@code origin} is not finite, or {@code bound} is not finite, or {@code origin} is
  386.      * greater than or equal to {@code bound}.
  387.      * @since 1.5
  388.      */
  389.     default DoubleStream doubles(long streamSize, double origin, double bound) {
  390.         UniformRandomProviderSupport.validateStreamSize(streamSize);
  391.         UniformRandomProviderSupport.validateRange(origin, bound);
  392.         return doubles(origin, bound).limit(streamSize);
  393.     }
  394. }