newrandom/JumpableRng.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.math.BigInteger;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    28
import java.util.stream.Stream;
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 pseudorandom sequences of numbers (or Boolean values)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    33
 * and furthermore can easily <it>jump</it> forward (by a fixed amount)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    34
 * to a 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 JumpableRng} objects produced by iterative
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    37
 * jumping from a single original {@code JumpableRng} object are
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    38
 * statistically independent of one another and individually uniform.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    39
 * In practice, one must settle for some approximation to independence
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    40
 * and uniformity.  In particular, a specific implementation may
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    41
 * assume that each generator in a stream produced by the {@code jumps}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    42
 * method is used to produce a number of values no larger than either
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    43
 * 2<sup>64</sup> or the square root of its period.  Implementors are
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    44
 * advised to use algorithms whose period is at least 2<sup>127</sup>.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    45
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    46
 * <p>Methods are provided to perform a single jump operation and also
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    47
 * to produce a stream of generators produced from the original by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    48
 * iterative copying and jumping of internal state.  A typical
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    49
 * strategy for a multithreaded application is to create a single
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    50
 * {@code JumpableRng} object, calls its {@code jumps} method exactly
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    51
 * once, and then parcel out generators from the resulting stream, one
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    52
 * to each thread.  It is generally not a good idea to call {@code jump}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    53
 * on a generator that was itself produced by the {@code jumps} method,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    54
 * because the result may be a generator identical to another
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    55
 * generator already produce by that call to the {@code jumps} method.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    56
 * For this reason, the return type of the {@code jumps} method is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    57
 * {@code Stream<Rng>} rather than {@code Stream<JumpableRng>}, even
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    58
 * though the actual generator objects in that stream likely do also
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    59
 * implement the {@code JumpableRng} interface.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    60
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    61
 * <p>An implementation of the {@code JumpableRng} interface must provide
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    62
 * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    63
 * {@code period()}, {@code copy()}, {@code jump()}, and {@code defaultJumpDistance()}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    64
 * Default implementations are provided for all other methods.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    65
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    66
 * <p>Objects that implement {@code java.util.JumpableRng} are
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    67
 * typically not cryptographically secure.  Consider instead using
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    68
 * {@link java.security.SecureRandom} to get a cryptographically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    69
 * secure pseudo-random number generator for use by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    70
 * security-sensitive applications.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    71
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    72
 * @author  Guy Steele
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    73
 * @since   1.9
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    74
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    75
interface JumpableRng extends StreamableRng {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    76
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    77
     * Returns a new generator whose internal state is an exact copy
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    78
     * of this generator (therefore their future behavior should be
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    79
     * identical if subjected to the same series of operations).
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    80
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    81
     * @return a new object that is a copy of this generator
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    82
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    83
    JumpableRng copy();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    84
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    85
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    86
     * Alter the state of this pseudorandom number generator so as to
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    87
     * jump forward a large, fixed distance (typically 2<sup>64</sup>
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    88
     * or more) within its state cycle.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    89
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    90
    void jump();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    91
 
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    92
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    93
     * Returns the distance by which the {@code jump()} method will jump
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    94
     * forward within the state cycle of this generator object.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    95
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    96
     * @return the default jump distance (as a {@code double} value)
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    97
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    98
    double defaultJumpDistance();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    99
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   100
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   101
     * Returns an effectively unlimited stream of new pseudorandom
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   102
     * number generators, each of which implements the {@code Rng}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   103
     * interface.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   104
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   105
     * @implNote It is permitted to implement this method in a manner
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   106
     * equivalent to {@code jumps(Long.MAX_VALUE)}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   107
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   108
     * @implNote The default implementation produces a sequential stream
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   109
     * that  repeatedly calls {@code copy()} and {@code jump()} on this generator,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   110
     * and the copies become the generators produced by the stream.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   111
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   112
     * @return a stream of objects that implement the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   113
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   114
    default Stream<Rng> jumps() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   115
	return Stream.generate(this::copyAndJump).sequential();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   116
    }
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
     * Returns a stream producing the given {@code streamSize} number of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   120
     * new pseudorandom number generators, each of which implements the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   121
     * {@code Rng} interface.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   122
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   123
     * @implNote The default implementation produces a sequential stream
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   124
     * that  repeatedly calls {@code copy()} and {@code jump()} on this generator,
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   125
     * and the copies become the generators produced by the stream.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   126
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   127
     * @param streamSize the number of generators to generate
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   128
     * @return a stream of objects that implement the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   129
     * @throws IllegalArgumentException if {@code streamSize} is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   130
     *         less than zero
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   131
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   132
    default Stream<Rng> jumps(long streamSize) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   133
        return jumps().limit(streamSize);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   134
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   135
    
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   136
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   137
     * Returns an effectively unlimited stream of new pseudorandom
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   138
     * number generators, each of which implements the {@code Rng}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   139
     * interface.  Ideally the generators in the stream will appear
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   140
     * to be statistically independent.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   141
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   142
     * @implNote The default implementation calls {@code jumps()}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   143
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   144
     * @return a stream of objects that implement the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   145
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   146
    default Stream<Rng> rngs() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   147
	return this.jumps();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   148
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   149
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   150
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   151
     * Returns a stream producing the given {@code streamSize} number of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   152
     * new pseudorandom number generators, each of which implements the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   153
     * {@code Rng} interface.  Ideally the generators in the stream will
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   154
     * appear to be statistically independent.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   155
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   156
     * @implNote The default implementation calls {@code jumps(streamSize)}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   157
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   158
     * @param streamSize the number of generators to generate
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   159
     * @return a stream of objects that implement the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   160
     * @throws IllegalArgumentException if {@code streamSize} is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   161
     *         less than zero
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   162
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   163
    default Stream<Rng> rngs(long streamSize) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   164
	return this.jumps(streamSize);
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
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   168
     * Copy this generator, jump this generator forward, then return the copy.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   169
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   170
    default Rng copyAndJump() {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   171
	Rng result = copy();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   172
	jump();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   173
	return result;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   174
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   175
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   176
}