newrandom/ArbitrarilyJumpableRng.java
author briangoetz
Thu, 23 May 2019 16:45:56 -0400
branchbriangoetz-test-branch
changeset 57369 6d87e9f7a1ec
permissions -rwxr-xr-x
Initial comment in newrandom/
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57369
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     1
/*
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     3
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     4
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     5
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     6
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     7
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     8
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     9
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    10
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    11
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    12
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    13
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    14
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    15
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    16
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    17
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    18
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    19
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    20
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    21
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    22
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    23
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    24
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    25
// package java.util;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    26
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    27
import java.util.stream.Stream;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    28
import java.util.stream.StreamSupport;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    29
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    30
/**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    31
 * This interface is designed to provide a common protocol for objects
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    32
 * that generate sequences of pseudorandom numbers (or Boolean values)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    33
 * and furthermore can easily <it>jump</it> to an arbitrarily specified
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    34
 * distant point in the state cycle.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    35
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    36
 * <p>Ideally, all {@code ArbitrarilyJumpableRng} objects produced by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    37
 * iterative jumping from a single original {@code ArbtrarilyJumpableRng}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    38
 * object are statistically independent of one another and
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    39
 * individually uniform, provided that they do not traverse
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    40
 * overlapping portions of the state cycle.  In practice, one must
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    41
 * settle for some approximation to independence and uniformity.  In
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    42
 * particular, a specific implementation may assume that each
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    43
 * generator in a stream produced by the {@code jumps} method is used
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    44
 * to produce a number of values no larger than the jump distance
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    45
 * specified.  Implementors are advised to use algorithms whose period
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    46
 * is at least 2<sup>127</sup>.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    47
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    48
 * <p>For many applications, it suffices to jump forward by a power of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    49
 * two or some small multiple of a power of two, but this power of two
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    50
 * may not be representable as a {@code long} value.  To avoid the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    51
 * use of {@code BigInteger} values as jump distances, {@code double}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    52
 * values are used instead.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    53
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    54
 * <p>Methods are provided to perform a single jump operation and also
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    55
 * to produce a stream of generators produced from the original by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    56
 * iterative copying and jumping of internal state.  A typical
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    57
 * strategy for a multithreaded application is to create a single
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    58
 * {@code ArbitrarilyJumpableRng} object, call its {@code jumps}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    59
 * method exactly once, and then parcel out generators from the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    60
 * resulting stream, one to each thread.  However, each generator
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    61
 * produced also has type {@code ArbitrarilyJumpableRng}; with care,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    62
 * different jump distances can be used to traverse the entire
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    63
 * state cycle in various ways.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    64
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    65
 * <p>An implementation of the {@code ArbitrarilyJumpableRng} interface must
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    66
 * provide concrete definitions for the methods {@code nextInt()},
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    67
 * {@code nextLong}, {@code period()}, {@code copy()}, {@code jump(double)},
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    68
 * {@code defaultJumpDistance()}, and {@code defaultLeapDistance()}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    69
 * Default implementations are provided for all other methods.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    70
 * Perhaps the most convenient
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    71
 * way to implement this interface is to extend the abstract class
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    72
 * {@link java.util.AbstractArbitrarilyJumpableRng}, which provides
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    73
 * spliterator-based implementations of the methods {@code ints}, {@code longs},
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    74
 * {@code doubles}, {@code rngs}, {@code jumps}, and {@code leaps}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    75
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    76
 * <p>Objects that implement {@code java.util.ArbitrarilyJumpableRng}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    77
 * are typically not cryptographically secure.  Consider instead using
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    78
 * {@link java.security.SecureRandom} to get a cryptographically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    79
 * secure pseudo-random number generator for use by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    80
 * security-sensitive applications.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    81
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    82
 * @author  Guy Steele
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    83
 * @since   1.9
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    84
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    85
interface ArbitrarilyJumpableRng extends LeapableRng {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    86
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    87
     * Returns a new generator whose internal state is an exact copy
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    88
     * of this generator (therefore their future behavior should be
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    89
     * identical if subjected to the same series of operations).
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    90
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    91
     * @return a new object that is a copy of this generator
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    92
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    93
    ArbitrarilyJumpableRng copy();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    94
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    95
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    96
     * Alter the state of this pseudorandom number generator so as to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    97
     * jump forward a distance equal to 2<sup>{@code logDistance}</sup>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    98
     * within its state cycle.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    99
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   100
     * @param logDistance the base-2 logarithm of the distance to jump
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   101
     *        forward within the state cycle
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   102
     * @throws IllegalArgumentException if {@code logDistance} is NaN
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   103
     *         or negative, or if 2<sup>{@code logDistance}</sup> is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   104
     *         greater than the period of this generator
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   105
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   106
    void jumpPowerOfTwo(int logDistance);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   107
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   108
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   109
     * Alter the state of this pseudorandom number generator so as to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   110
     * jump forward a specified distance within its state cycle.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   111
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   112
     * @param distance the distance to jump forward within the state cycle
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   113
     * @throws IllegalArgumentException if {@code distance} is Nan,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   114
     *         negative, or greater than the period of this generator
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   115
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   116
    void jump(double distance);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   117
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   118
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   119
     * Alter the state of this pseudorandom number generator so as to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   120
     * jump forward a large, fixed distance (typically 2<sup>64</sup>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   121
     * or more) within its state cycle.  The distance used is that
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   122
     * returned by method {@code defaultJumpDistance()}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   123
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   124
    default void jump() { jump(defaultJumpDistance()); }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   125
    
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   126
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   127
     * Returns an effectively unlimited stream of new pseudorandom
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   128
     * number generators, each of which implements the {@code ArbitrarilyJumpableRng}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   129
     * interface, produced by jumping copies of this generator
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   130
     * by different integer multiples of the specified jump distance.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   131
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   132
     * @implNote This method is implemented to be equivalent to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   133
     * {@code jumps(Long.MAX_VALUE)}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   134
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   135
     * @param distance a distance to jump forward within the state cycle
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   136
     * @return a stream of objects that implement the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   137
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   138
    default Stream<ArbitrarilyJumpableRng> jumps(double distance) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   139
	return Stream.generate(() -> copyAndJump(distance)).sequential();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   140
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   141
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   142
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   143
     * Returns a stream producing the given {@code streamSize} number of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   144
     * new pseudorandom number generators, each of which implements the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   145
     * {@code ArbitrarilyJumpableRng} interface, produced by jumping copies of this generator
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   146
     * by different integer multiples of the specified jump distance.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   147
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   148
     * @param streamSize the number of generators to generate
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   149
     * @param distance a distance to jump forward within the state cycle
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   150
     * @return a stream of objects that implement the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   151
     * @throws IllegalArgumentException if {@code streamSize} is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   152
     *         less than zero
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   153
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   154
    default Stream<ArbitrarilyJumpableRng> jumps(long streamSize, double distance) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   155
        return jumps(distance).limit(streamSize);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   156
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   157
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   158
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   159
     * Alter the state of this pseudorandom number generator so as to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   160
     * jump forward a very large, fixed distance (typically 2<sup>128</sup>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   161
     * or more) within its state cycle.  The distance used is that
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   162
     * returned by method {@code defaultJLeapDistance()}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   163
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   164
    default void leap() { jump(defaultLeapDistance()); }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   165
     
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   166
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   167
     * Copy this generator, jump this generator forward, then return the copy.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   168
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   169
    default ArbitrarilyJumpableRng copyAndJump(double distance) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   170
	ArbitrarilyJumpableRng result = copy();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   171
	jump(distance);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   172
	return result;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   173
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   174
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   175
}