src/java.base/share/classes/java/util/random/LeapableRNG.java
branchJDK-8193209-branch
changeset 57547 56cbdc3ea079
parent 57546 1ca1cfdcb451
child 57671 6a4be8bf8990
--- a/src/java.base/share/classes/java/util/random/LeapableRNG.java	Fri Jul 26 15:20:31 2019 -0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,135 +0,0 @@
-/*
- * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package java.util.random;
-
-import java.util.stream.Stream;
-
-/**
- * This interface is designed to provide a common protocol for objects that generate sequences of
- * pseudorandom numbers (or Boolean values) and furthermore can easily not only jump but also
- * <i>leap</i> to a very distant point in the state cycle.
- * <p>
- * Typically one will construct a series of {@link LeapableRNG} objects by iterative leaping from a
- * single original {@link LeapableRNG} object, and then for each such object produce a subseries of
- * objects by iterative jumping.  There is little conceptual difference between leaping and jumping,
- * but typically a leap will be a very long jump in the state cycle (perhaps distance
- * 2<sup>128</sup> or so).
- * <p>
- * Ideally, all {@link LeapableRNG} objects produced by iterative leaping and jumping from a single
- * original {@link LeapableRNG} object are statistically independent of one another and individually
- * uniform. In practice, one must settle for some approximation to independence and uniformity.  In
- * particular, a specific implementation may assume that each generator in a stream produced by the
- * {@code leaps} method is used to produce (by jumping) a number of objects no larger than
- * 2<sup>64</sup>.  Implementors are advised to use algorithms whose period is at least
- * 2<sup>191</sup>.
- * <p>
- * Methods are provided to perform a single leap operation and also to produce a stream of
- * generators produced from the original by iterative copying and leaping of internal state.  The
- * generators produced must implement the {@link JumpableRNG} interface but need not also implement
- * the {@link LeapableRNG} interface.  A typical strategy for a multithreaded application is to
- * create a single {@link LeapableRNG} object, calls its {@code leaps} method exactly once, and then
- * parcel out generators from the resulting stream, one to each thread.  Then the {@code jumps}
- * method of each such generator be called to produce a substream of generator objects.
- * <p>
- * An implementation of the {@link LeapableRNG} interface must provide concrete definitions for the
- * methods {@code nextInt()}, {@code nextLong}, {@code period()}, {@code copy()}, {@code jump()},
- * {@code defaultJumpDistance()}, {@code leap()}, and {@code defaultLeapDistance()}. Default
- * implementations are provided for all other methods.
- * <p>
- * Objects that implement {@link LeapableRNG} are typically not cryptographically secure.  Consider
- * instead using {@link java.security.SecureRandom} to get a cryptographically secure pseudo-random
- * number generator for use by security-sensitive applications.
- *
- * @since 14
- */
-public interface LeapableRNG extends JumpableRNG {
-    /**
-     * Returns a new generator whose internal state is an exact copy of this generator (therefore
-     * their future behavior should be identical if subjected to the same series of operations).
-     *
-     * @return a new object that is a copy of this generator
-     */
-    LeapableRNG copy();
-
-    /**
-     * Alter the state of this pseudorandom number generator so as to leap forward a large, fixed
-     * distance (typically 2<sup>96</sup> or more) within its state cycle.
-     */
-    void leap();
-
-    /**
-     * Returns the distance by which the {@code leap()} method will leap forward within the state
-     * cycle of this generator object.
-     *
-     * @return the default leap distance (as a {@code double} value)
-     */
-    double defaultLeapDistance();
-
-    /**
-     * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
-     * implements the {@link JumpableRNG} interface.
-     *
-     * @return a stream of objects that implement the {@link JumpableRNG} interface
-     *
-     * @implNote It is permitted to implement this method in a manner equivalent to {@code
-     *         leaps(Long.MAX_VALUE)}.
-     * @implNote The default implementation produces a sequential stream that  repeatedly
-     *         calls {@code copy()} and {@code leap()} on this generator, and the copies become the
-     *         generators produced by the stream.
-     */
-    default Stream<JumpableRNG> leaps() {
-        return Stream.generate(this::copyAndLeap).sequential();
-    }
-
-    /**
-     * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
-     * generators, each of which implements the {@link JumpableRNG} interface.
-     *
-     * @param streamSize the number of generators to generate
-     *
-     * @return a stream of objects that implement the {@link JumpableRNG} interface
-     *
-     * @throws IllegalArgumentException if {@code streamSize} is less than zero
-     * @implNote The default implementation produces a sequential stream that  repeatedly
-     *         calls {@code copy()} and {@code leap()} on this generator, and the copies become the
-     *         generators produced by the stream.
-     */
-    default Stream<JumpableRNG> leaps(long streamSize) {
-        return leaps().limit(streamSize);
-    }
-
-    /**
-     * Copy this generator, leap this generator forward, then return the copy.
-     *
-     * @return a copy of this generator object before the leap occurred
-     */
-    default JumpableRNG copyAndLeap() {
-        JumpableRNG result = copy();
-        leap();
-        return result;
-    }
-
-}