src/java.base/share/classes/java/util/random/RandomGenerator.java
branchJDK-8193209-branch
changeset 57684 7cb325557832
parent 57671 6a4be8bf8990
child 57940 7e791393cc4d
equal deleted inserted replaced
57671:6a4be8bf8990 57684:7cb325557832
    31 import java.util.stream.IntStream;
    31 import java.util.stream.IntStream;
    32 import java.util.stream.LongStream;
    32 import java.util.stream.LongStream;
    33 import java.util.stream.Stream;
    33 import java.util.stream.Stream;
    34 
    34 
    35 /**
    35 /**
    36  * The {@link RandomGenerator} interface is designed to provide a common protocol for objects
    36  * The {@link RandomGenerator} interface is designed to provide a common protocol for objects that
    37  * that generate random or (more typically) pseudorandom sequences of numbers (or Boolean values).
    37  * generate random or (more typically) pseudorandom sequences of numbers (or Boolean values). Such a
    38  * Such a sequence may be obtained by either repeatedly invoking a method that returns a single
    38  * sequence may be obtained by either repeatedly invoking a method that returns a single
    39  * (pseudo)randomly chosen value, or by invoking a method that returns a stream of (pseudo)randomly
    39  * (pseudo)randomly chosen value, or by invoking a method that returns a stream of (pseudo)randomly
    40  * chosen values.
    40  * chosen values.
    41  * <p>
    41  * <p>
    42  * Ideally, given an implicitly or explicitly specified range of values, each value would be chosen
    42  * Ideally, given an implicitly or explicitly specified range of values, each value would be chosen
    43  * independently and uniformly from that range. In practice, one may have to settle for some
    43  * independently and uniformly from that range. In practice, one may have to settle for some
    51  * 2<sup>&minus;<i>w</i></sup>; if an explicit range is specified, then the chosen number is
    51  * 2<sup>&minus;<i>w</i></sup>; if an explicit range is specified, then the chosen number is
    52  * computationally scaled and translated so as to appear to have been chosen from that range.
    52  * computationally scaled and translated so as to appear to have been chosen from that range.
    53  * <p>
    53  * <p>
    54  * Each method that returns a stream produces a stream of values each of which is chosen in the same
    54  * Each method that returns a stream produces a stream of values each of which is chosen in the same
    55  * manner as for a method that returns a single (pseudo)randomly chosen value.  For example, if
    55  * manner as for a method that returns a single (pseudo)randomly chosen value.  For example, if
    56  * {@code r} implements {@link RandomGenerator}, then the method call {@code r.ints(100)}
    56  * {@code r} implements {@link RandomGenerator}, then the method call {@code r.ints(100)} returns a
    57  * returns a stream of 100 {@code int} values.  These are not necessarily the exact same values that
    57  * stream of 100 {@code int} values.  These are not necessarily the exact same values that would
    58  * would have been returned if instead {@code r.nextInt()} had been called 100 times; all that is
    58  * have been returned if instead {@code r.nextInt()} had been called 100 times; all that is
    59  * guaranteed is that each value in the stream is chosen in a similar (pseudo)random manner from the
    59  * guaranteed is that each value in the stream is chosen in a similar (pseudo)random manner from the
    60  * same range.
    60  * same range.
    61  * <p>
    61  * <p>
    62  * Every object that implements the {@link RandomGenerator} interface is assumed to contain a
    62  * Every object that implements the {@link RandomGenerator} interface is assumed to contain a finite
    63  * finite amount of state.  Using such an object to generate a pseudorandomly chosen value alters
    63  * amount of state.  Using such an object to generate a pseudorandomly chosen value alters its
    64  * its state.  The number of distinct possible states of such an object is called its
    64  * state.  The number of distinct possible states of such an object is called its
    65  * <i>period</i>.  (Some implementations of the {@link RandomGenerator} interface
    65  * <i>period</i>.  (Some implementations of the {@link RandomGenerator} interface
    66  * may be truly random rather than pseudorandom, for example relying on the statistical behavior of
    66  * may be truly random rather than pseudorandom, for example relying on the statistical behavior of
    67  * a physical object to derive chosen values.  Such implementations do not have a fixed period.)
    67  * a physical object to derive chosen values.  Such implementations do not have a fixed period.)
    68  * <p>
    68  * <p>
    69  * As a rule, objects that implement the {@link RandomGenerator} interface need not be
    69  * As a rule, objects that implement the {@link RandomGenerator} interface need not be thread-safe.
    70  * thread-safe.  It is recommended that multithreaded applications use either {@link
    70  * It is recommended that multithreaded applications use either {@link ThreadLocalRandom} or
    71  * ThreadLocalRandom} or (preferably) pseudorandom number generators that implement the {@link
    71  * (preferably) pseudorandom number generators that implement the {@link SplittableGenerator} or
    72  * SplittableGenerator} or {@link JumpableGenerator} interface.
    72  * {@link JumpableGenerator} interface.
    73  * <p>
    73  * <p>
    74  * To implement this interface, a class only needs to provide concrete definitions for the methods
    74  * To implement this interface, a class only needs to provide concrete definitions for the methods
    75  * {@code nextLong()} and {@code period()}. Default implementations are provided for all other
    75  * {@code nextLong()} and {@code period()}. Default implementations are provided for all other
    76  * methods (but it may be desirable to override some of them, especially {@code nextInt()} if the
    76  * methods (but it may be desirable to override some of them, especially {@code nextInt()} if the
    77  * underlying algorithm is {@code int}-based). Moerover, it may be preferable instead to implement
    77  * underlying algorithm is {@code int}-based). Moerover, it may be preferable instead to implement
    78  * another interface such as {@link JumpableGenerator} or {@link LeapableGenerator}, or to extend an abstract
    78  * another interface such as {@link JumpableGenerator} or {@link LeapableGenerator}, or to extend an
    79  * class such as {@link AbstractSplittableGenerator} or {@link AbstractArbitrarilyJumpableGenerator}.
    79  * abstract class such as {@link AbstractSplittableGenerator} or {@link
       
    80  * AbstractArbitrarilyJumpableGenerator}.
    80  * <p>
    81  * <p>
    81  * Objects that implement {@link RandomGenerator} are typically not cryptographically secure.
    82  * Objects that implement {@link RandomGenerator} are typically not cryptographically secure.
    82  * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
    83  * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
    83  * pseudorandom number generator for use by security-sensitive applications.  Note, however, that
    84  * pseudorandom number generator for use by security-sensitive applications.  Note, however, that
    84  * {@code java.security.SecureRandom} does implement the {@link RandomGenerator} interface, so
    85  * {@code java.security.SecureRandom} does implement the {@link RandomGenerator} interface, so that
    85  * that instances of {@code java.security.SecureRandom} may be used interchangeably with other types
    86  * instances of {@code java.security.SecureRandom} may be used interchangeably with other types of
    86  * of pseudorandom generators in applications that do not require a secure generator.
    87  * pseudorandom generators in applications that do not require a secure generator.
    87  *
    88  *
    88  * @since 14
    89  * @since 14
    89  */
    90  */
    90 public interface RandomGenerator {
    91 public interface RandomGenerator {
    91 
    92 
   142          */
   143          */
   143         Xoshiro256StarStar("Xoshiro256StarStar");
   144         Xoshiro256StarStar("Xoshiro256StarStar");
   144 
   145 
   145         private String name;
   146         private String name;
   146 
   147 
   147         Algorithm(String name) {
   148         private Algorithm(String name) {
   148             this.name = name;
   149             this.name = name;
   149         }
   150         }
   150 
   151 
   151         public String toString() {
   152         public String toString() {
   152             return name;
   153             return name;
       
   154         }
       
   155 
       
   156         /**
       
   157          * Returns an instance of {@link RandomGenerator} that utilizes this algorithm.
       
   158          *
       
   159          * @return An instance of {@link RandomGenerator}
       
   160          */
       
   161         public RandomGenerator instance() {
       
   162             return RandomGeneratorFactory.of(name, RandomGenerator.class);
       
   163         }
       
   164 
       
   165         /**
       
   166          * Returns a {@link RandomGeneratorFactory} that can produce instances
       
   167          * of {@link RandomGenerator} that utilizes this algorithm.
       
   168          *
       
   169          * @return {@link RandomGeneratorFactory} of {@link RandomGenerator}
       
   170          */
       
   171         public RandomGeneratorFactory<RandomGenerator> factory() {
       
   172             return RandomGeneratorFactory.factoryOf(name, RandomGenerator.class);
   153         }
   173         }
   154     }
   174     }
   155 
   175 
   156     /**
   176     /**
   157      * Returns an instance of {@link RandomGenerator} that utilizes the
   177      * Returns an instance of {@link RandomGenerator} that utilizes the
   183      * Returns a {@link RandomGeneratorFactory} that can produce instances
   203      * Returns a {@link RandomGeneratorFactory} that can produce instances
   184      * of {@link RandomGenerator} that utilizes the {@code name} algorithm.
   204      * of {@link RandomGenerator} that utilizes the {@code name} algorithm.
   185      *
   205      *
   186      * @param name  Name of random number generator algorithm
   206      * @param name  Name of random number generator algorithm
   187      *
   207      *
   188      * @return Factory of {@link RandomGenerator}
   208      * @return {@link RandomGeneratorFactory} of {@link RandomGenerator}
   189      */
   209      */
   190     public static RandomGeneratorFactory<RandomGenerator> factoryOf(String name) {
   210     public static RandomGeneratorFactory<RandomGenerator> factoryOf(String name) {
   191         Objects.requireNonNull(name);
   211         Objects.requireNonNull(name);
   192         return RandomGeneratorFactory.factoryOf(name, RandomGenerator.class);
   212         return RandomGeneratorFactory.factoryOf(name, RandomGenerator.class);
   193     }
   213     }
   196      * Returns a {@link RandomGeneratorFactory} that can produce instances
   216      * Returns a {@link RandomGeneratorFactory} that can produce instances
   197      * of {@link RandomGenerator} that utilizes the specified {@code algorithm}.
   217      * of {@link RandomGenerator} that utilizes the specified {@code algorithm}.
   198      *
   218      *
   199      * @param algorithm  Random number generator algorithm
   219      * @param algorithm  Random number generator algorithm
   200      *
   220      *
   201      * @return Factory of {@link RandomGenerator}
   221      * @return {@link RandomGeneratorFactory} of {@link RandomGenerator}
   202      */
   222      */
   203     public static RandomGeneratorFactory<RandomGenerator> factoryOf(Algorithm algorithm) {
   223     public static RandomGeneratorFactory<RandomGenerator> factoryOf(Algorithm algorithm) {
   204         Objects.requireNonNull(algorithm);
   224         Objects.requireNonNull(algorithm);
   205         return RandomGeneratorFactory.factoryOf(algorithm.toString(), RandomGenerator.class);
   225         return RandomGeneratorFactory.factoryOf(algorithm.toString(), RandomGenerator.class);
   206     }
   226     }
   819          * Returns a {@link RandomGeneratorFactory} that can produce instances
   839          * Returns a {@link RandomGeneratorFactory} that can produce instances
   820          * of {@link StreamableGenerator} that utilizes the {@code name} algorithm.
   840          * of {@link StreamableGenerator} that utilizes the {@code name} algorithm.
   821          *
   841          *
   822          * @param name  Name of random number generator algorithm
   842          * @param name  Name of random number generator algorithm
   823          *
   843          *
   824          * @return Factory of {@link StreamableGenerator}
   844          * @return {@link RandomGeneratorFactory} of {@link StreamableGenerator}
   825          */
   845          */
   826         public static RandomGeneratorFactory<StreamableGenerator> factoryOf(String name) {
   846         public static RandomGeneratorFactory<StreamableGenerator> factoryOf(String name) {
   827             Objects.requireNonNull(name);
   847             Objects.requireNonNull(name);
   828             return RandomGeneratorFactory.factoryOf(name, StreamableGenerator.class);
   848             return RandomGeneratorFactory.factoryOf(name, StreamableGenerator.class);
   829         }
   849         }
   832          * Returns a {@link RandomGeneratorFactory} that can produce instances
   852          * Returns a {@link RandomGeneratorFactory} that can produce instances
   833          * of {@link StreamableGenerator} that utilizes the specified {@code algorithm}.
   853          * of {@link StreamableGenerator} that utilizes the specified {@code algorithm}.
   834          *
   854          *
   835          * @param algorithm  Random number generator algorithm
   855          * @param algorithm  Random number generator algorithm
   836          *
   856          *
   837          * @return Factory of {@link StreamableGenerator}
   857          * @return {@link RandomGeneratorFactory} of {@link StreamableGenerator}
   838          */
   858          */
   839         public static RandomGeneratorFactory<StreamableGenerator> factoryOf(Algorithm algorithm) {
   859         public static RandomGeneratorFactory<StreamableGenerator> factoryOf(Algorithm algorithm) {
   840             Objects.requireNonNull(algorithm);
   860             Objects.requireNonNull(algorithm);
   841             return RandomGeneratorFactory.factoryOf(algorithm.toString(), StreamableGenerator.class);
   861             return RandomGeneratorFactory.factoryOf(algorithm.toString(), StreamableGenerator.class);
   842         }
   862         }
   946          * Returns a {@link RandomGeneratorFactory} that can produce instances
   966          * Returns a {@link RandomGeneratorFactory} that can produce instances
   947          * of {@link SplittableGenerator} that utilizes the {@code name} algorithm.
   967          * of {@link SplittableGenerator} that utilizes the {@code name} algorithm.
   948          *
   968          *
   949          * @param name  Name of random number generator algorithm
   969          * @param name  Name of random number generator algorithm
   950          *
   970          *
   951          * @return Factory of {@link SplittableGenerator}
   971          * @return {@link RandomGeneratorFactory} of {@link SplittableGenerator}
   952          */
   972          */
   953         public static RandomGeneratorFactory<SplittableGenerator> factoryOf(String name) {
   973         public static RandomGeneratorFactory<SplittableGenerator> factoryOf(String name) {
   954             Objects.requireNonNull(name);
   974             Objects.requireNonNull(name);
   955             return RandomGeneratorFactory.factoryOf(name, SplittableGenerator.class);
   975             return RandomGeneratorFactory.factoryOf(name, SplittableGenerator.class);
   956         }
   976         }
   959          * Returns a {@link RandomGeneratorFactory} that can produce instances
   979          * Returns a {@link RandomGeneratorFactory} that can produce instances
   960          * of {@link SplittableGenerator} that utilizes the specified {@code algorithm}.
   980          * of {@link SplittableGenerator} that utilizes the specified {@code algorithm}.
   961          *
   981          *
   962          * @param algorithm  Random number generator algorithm
   982          * @param algorithm  Random number generator algorithm
   963          *
   983          *
   964          * @return Factory of {@link SplittableGenerator}
   984          * @return {@link RandomGeneratorFactory} of {@link SplittableGenerator}
   965          */
   985          */
   966         public static RandomGeneratorFactory<SplittableGenerator> factoryOf(Algorithm algorithm) {
   986         public static RandomGeneratorFactory<SplittableGenerator> factoryOf(Algorithm algorithm) {
   967             Objects.requireNonNull(algorithm);
   987             Objects.requireNonNull(algorithm);
   968             return RandomGeneratorFactory.factoryOf(algorithm.toString(), SplittableGenerator.class);
   988             return RandomGeneratorFactory.factoryOf(algorithm.toString(), SplittableGenerator.class);
   969         }
   989         }
  1095             return this.splits(streamSize).map(x -> (RandomGenerator)x);
  1115             return this.splits(streamSize).map(x -> (RandomGenerator)x);
  1096         }
  1116         }
  1097     }
  1117     }
  1098 
  1118 
  1099     /**
  1119     /**
  1100      * This interface is designed to provide a common protocol for objects that generate pseudorandom
  1120      * This interface is designed to provide a common protocol for objects that generate
  1101      * sequences of numbers (or Boolean values) and furthermore can easily <i>jump</i> forward (by a
  1121      * pseudorandom sequences of numbers (or Boolean values) and furthermore can easily <i>jump</i>
  1102      * fixed amount) to a distant point in the state cycle.
  1122      * forward (by a fixed amount) to a distant point in the state cycle.
  1103      * <p>
  1123      * <p>
  1104      * Ideally, all {@link JumpableGenerator} objects produced by iterative jumping from a single original
  1124      * Ideally, all {@link JumpableGenerator} objects produced by iterative jumping from a single
  1105      * {@link JumpableGenerator} object are statistically independent of one another and individually uniform.
  1125      * original {@link JumpableGenerator} object are statistically independent of one another and
  1106      * In practice, one must settle for some approximation to independence and uniformity.  In
  1126      * individually uniform. In practice, one must settle for some approximation to independence and
  1107      * particular, a specific implementation may assume that each generator in a stream produced by the
  1127      * uniformity.  In particular, a specific implementation may assume that each generator in a
  1108      * {@code jumps} method is used to produce a number of values no larger than either 2<sup>64</sup>
  1128      * stream produced by the {@code jumps} method is used to produce a number of values no larger
  1109      * or the square root of its period.  Implementors are advised to use algorithms whose period is at
  1129      * than either 2<sup>64</sup> or the square root of its period.  Implementors are advised to use
  1110      * least 2<sup>127</sup>.
  1130      * algorithms whose period is at least 2<sup>127</sup>.
  1111      * <p>
  1131      * <p>
  1112      * Methods are provided to perform a single jump operation and also to produce a stream of
  1132      * Methods are provided to perform a single jump operation and also to produce a stream of
  1113      * generators produced from the original by iterative copying and jumping of internal state.  A
  1133      * generators produced from the original by iterative copying and jumping of internal state.  A
  1114      * typical strategy for a multithreaded application is to create a single {@link JumpableGenerator}
  1134      * typical strategy for a multithreaded application is to create a single {@link
  1115      * object, calls its {@code jumps} method exactly once, and then parcel out generators from the
  1135      * JumpableGenerator} object, calls its {@code jumps} method exactly once, and then parcel out
  1116      * resulting stream, one to each thread.  It is generally not a good idea to call {@code jump} on a
  1136      * generators from the resulting stream, one to each thread.  It is generally not a good idea to
  1117      * generator that was itself produced by the {@code jumps} method, because the result may be a
  1137      * call {@code jump} on a generator that was itself produced by the {@code jumps} method,
  1118      * generator identical to another generator already produce by that call to the {@code jumps}
  1138      * because the result may be a generator identical to another generator already produce by that
  1119      * method. For this reason, the return type of the {@code jumps} method is {@code
  1139      * call to the {@code jumps} method. For this reason, the return type of the {@code jumps}
  1120      * Stream<RandomGenerator>} rather than {@code Stream<JumpableGenerator>}, even though the actual
  1140      * method is {@code Stream<RandomGenerator>} rather than {@code Stream<JumpableGenerator>}, even
  1121      * generator objects in that stream likely do also implement the {@link JumpableGenerator} interface.
  1141      * though the actual generator objects in that stream likely do also implement the {@link
  1122      * <p>
  1142      * JumpableGenerator} interface.
  1123      * An implementation of the {@link JumpableGenerator} interface must provide concrete definitions for the
  1143      * <p>
  1124      * methods {@code nextInt()}, {@code nextLong}, {@code period()}, {@code copy()}, {@code jump()},
  1144      * An implementation of the {@link JumpableGenerator} interface must provide concrete
  1125      * and {@code defaultJumpDistance()}. Default implementations are provided for all other methods.
  1145      * definitions for the methods {@code nextInt()}, {@code nextLong}, {@code period()}, {@code
  1126      * <p>
  1146      * copy()}, {@code jump()}, and {@code defaultJumpDistance()}. Default implementations are
  1127      * Objects that implement {@link JumpableGenerator} are typically not cryptographically secure.  Consider
  1147      * provided for all other methods.
  1128      * instead using {@link java.security.SecureRandom} to get a cryptographically secure pseudo-random
  1148      * <p>
  1129      * number generator for use by security-sensitive applications.
  1149      * Objects that implement {@link JumpableGenerator} are typically not cryptographically secure.
       
  1150      * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
       
  1151      * pseudo-random number generator for use by security-sensitive applications.
  1130      *
  1152      *
  1131      * @since 14
  1153      * @since 14
  1132      */
  1154      */
  1133     public interface JumpableGenerator extends StreamableGenerator {
  1155     public interface JumpableGenerator extends StreamableGenerator {
  1134 
  1156 
  1162          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1184          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1163          * of {@link JumpableGenerator} that utilizes the {@code name} algorithm.
  1185          * of {@link JumpableGenerator} that utilizes the {@code name} algorithm.
  1164          *
  1186          *
  1165          * @param name  Name of random number generator algorithm
  1187          * @param name  Name of random number generator algorithm
  1166          *
  1188          *
  1167          * @return Factory of {@link JumpableGenerator}
  1189          * @return {@link RandomGeneratorFactory} of {@link JumpableGenerator}
  1168          */
  1190          */
  1169         public static RandomGeneratorFactory<JumpableGenerator> factoryOf(String name) {
  1191         public static RandomGeneratorFactory<JumpableGenerator> factoryOf(String name) {
  1170             Objects.requireNonNull(name);
  1192             Objects.requireNonNull(name);
  1171             return RandomGeneratorFactory.factoryOf(name, JumpableGenerator.class);
  1193             return RandomGeneratorFactory.factoryOf(name, JumpableGenerator.class);
  1172         }
  1194         }
  1175          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1197          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1176          * of {@link JumpableGenerator} that utilizes the specified {@code algorithm}.
  1198          * of {@link JumpableGenerator} that utilizes the specified {@code algorithm}.
  1177          *
  1199          *
  1178          * @param algorithm  Random number generator algorithm
  1200          * @param algorithm  Random number generator algorithm
  1179          *
  1201          *
  1180          * @return Factory of {@link JumpableGenerator}
  1202          * @return {@link RandomGeneratorFactory} of {@link JumpableGenerator}
  1181          */
  1203          */
  1182         public static RandomGeneratorFactory<JumpableGenerator> factoryOf(Algorithm algorithm) {
  1204         public static RandomGeneratorFactory<JumpableGenerator> factoryOf(Algorithm algorithm) {
  1183             Objects.requireNonNull(algorithm);
  1205             Objects.requireNonNull(algorithm);
  1184             return RandomGeneratorFactory.factoryOf(algorithm.toString(), JumpableGenerator.class);
  1206             return RandomGeneratorFactory.factoryOf(algorithm.toString(), JumpableGenerator.class);
  1185         }
  1207         }
  1280         }
  1302         }
  1281 
  1303 
  1282     }
  1304     }
  1283 
  1305 
  1284     /**
  1306     /**
  1285      * This interface is designed to provide a common protocol for objects that generate sequences of
  1307      * This interface is designed to provide a common protocol for objects that generate sequences
  1286      * pseudorandom numbers (or Boolean values) and furthermore can easily not only jump but also
  1308      * of pseudorandom numbers (or Boolean values) and furthermore can easily not only jump but
       
  1309      * also
  1287      * <i>leap</i> to a very distant point in the state cycle.
  1310      * <i>leap</i> to a very distant point in the state cycle.
  1288      * <p>
  1311      * <p>
  1289      * Typically one will construct a series of {@link LeapableGenerator} objects by iterative leaping from a
  1312      * Typically one will construct a series of {@link LeapableGenerator} objects by iterative
  1290      * single original {@link LeapableGenerator} object, and then for each such object produce a subseries of
  1313      * leaping from a single original {@link LeapableGenerator} object, and then for each such
  1291      * objects by iterative jumping.  There is little conceptual difference between leaping and jumping,
  1314      * object produce a subseries of objects by iterative jumping.  There is little conceptual
  1292      * but typically a leap will be a very long jump in the state cycle (perhaps distance
  1315      * difference between leaping and jumping, but typically a leap will be a very long jump in the
  1293      * 2<sup>128</sup> or so).
  1316      * state cycle (perhaps distance 2<sup>128</sup> or so).
  1294      * <p>
  1317      * <p>
  1295      * Ideally, all {@link LeapableGenerator} objects produced by iterative leaping and jumping from a single
  1318      * Ideally, all {@link LeapableGenerator} objects produced by iterative leaping and jumping from
  1296      * original {@link LeapableGenerator} object are statistically independent of one another and individually
  1319      * a single original {@link LeapableGenerator} object are statistically independent of one
  1297      * uniform. In practice, one must settle for some approximation to independence and uniformity.  In
  1320      * another and individually uniform. In practice, one must settle for some approximation to
  1298      * particular, a specific implementation may assume that each generator in a stream produced by the
  1321      * independence and uniformity.  In particular, a specific implementation may assume that each
  1299      * {@code leaps} method is used to produce (by jumping) a number of objects no larger than
  1322      * generator in a stream produced by the {@code leaps} method is used to produce (by jumping) a
  1300      * 2<sup>64</sup>.  Implementors are advised to use algorithms whose period is at least
  1323      * number of objects no larger than 2<sup>64</sup>.  Implementors are advised to use algorithms
  1301      * 2<sup>191</sup>.
  1324      * whose period is at least 2<sup>191</sup>.
  1302      * <p>
  1325      * <p>
  1303      * Methods are provided to perform a single leap operation and also to produce a stream of
  1326      * Methods are provided to perform a single leap operation and also to produce a stream of
  1304      * generators produced from the original by iterative copying and leaping of internal state.  The
  1327      * generators produced from the original by iterative copying and leaping of internal state.
  1305      * generators produced must implement the {@link JumpableGenerator} interface but need not also implement
  1328      * The generators produced must implement the {@link JumpableGenerator} interface but need not
  1306      * the {@link LeapableGenerator} interface.  A typical strategy for a multithreaded application is to
  1329      * also implement the {@link LeapableGenerator} interface.  A typical strategy for a
  1307      * create a single {@link LeapableGenerator} object, calls its {@code leaps} method exactly once, and then
  1330      * multithreaded application is to create a single {@link LeapableGenerator} object, calls its
  1308      * parcel out generators from the resulting stream, one to each thread.  Then the {@code jumps}
  1331      * {@code leaps} method exactly once, and then parcel out generators from the resulting stream,
  1309      * method of each such generator be called to produce a substream of generator objects.
  1332      * one to each thread.  Then the {@code jumps} method of each such generator be called to
  1310      * <p>
  1333      * produce a substream of generator objects.
  1311      * An implementation of the {@link LeapableGenerator} interface must provide concrete definitions for the
  1334      * <p>
  1312      * methods {@code nextInt()}, {@code nextLong}, {@code period()}, {@code copy()}, {@code jump()},
  1335      * An implementation of the {@link LeapableGenerator} interface must provide concrete
  1313      * {@code defaultJumpDistance()}, {@code leap()}, and {@code defaultLeapDistance()}. Default
  1336      * definitions for the methods {@code nextInt()}, {@code nextLong}, {@code period()},
  1314      * implementations are provided for all other methods.
  1337      * {@code copy()}, {@code jump()}, {@code defaultJumpDistance()}, {@code leap()},
  1315      * <p>
  1338      * and {@code defaultLeapDistance()}. Default implementations are provided for all other
  1316      * Objects that implement {@link LeapableGenerator} are typically not cryptographically secure.  Consider
  1339      * methods.
  1317      * instead using {@link java.security.SecureRandom} to get a cryptographically secure pseudo-random
  1340      * <p>
  1318      * number generator for use by security-sensitive applications.
  1341      * Objects that implement {@link LeapableGenerator} are typically not cryptographically secure.
       
  1342      * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
       
  1343      * pseudo-random number generator for use by security-sensitive applications.
  1319      *
  1344      *
  1320      * @since 14
  1345      * @since 14
  1321      */
  1346      */
  1322     public interface LeapableGenerator extends JumpableGenerator {
  1347     public interface LeapableGenerator extends JumpableGenerator {
  1323 
  1348 
  1351          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1376          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1352          * of {@link LeapableGenerator} that utilizes the {@code name} algorithm.
  1377          * of {@link LeapableGenerator} that utilizes the {@code name} algorithm.
  1353          *
  1378          *
  1354          * @param name  Name of random number generator algorithm
  1379          * @param name  Name of random number generator algorithm
  1355          *
  1380          *
  1356          * @return Factory of {@link LeapableGenerator}
  1381          * @return {@link RandomGeneratorFactory} of {@link LeapableGenerator}
  1357          */
  1382          */
  1358         public static RandomGeneratorFactory<LeapableGenerator> factoryOf(String name) {
  1383         public static RandomGeneratorFactory<LeapableGenerator> factoryOf(String name) {
  1359             Objects.requireNonNull(name);
  1384             Objects.requireNonNull(name);
  1360             return RandomGeneratorFactory.factoryOf(name, LeapableGenerator.class);
  1385             return RandomGeneratorFactory.factoryOf(name, LeapableGenerator.class);
  1361         }
  1386         }
  1364          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1389          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1365          * of {@link LeapableGenerator} that utilizes the specified {@code algorithm}.
  1390          * of {@link LeapableGenerator} that utilizes the specified {@code algorithm}.
  1366          *
  1391          *
  1367          * @param algorithm  Random number generator algorithm
  1392          * @param algorithm  Random number generator algorithm
  1368          *
  1393          *
  1369          * @return Factory of {@link LeapableGenerator}
  1394          * @return {@link RandomGeneratorFactory} of {@link LeapableGenerator}
  1370          */
  1395          */
  1371         public static RandomGeneratorFactory<LeapableGenerator> factoryOf(Algorithm algorithm) {
  1396         public static RandomGeneratorFactory<LeapableGenerator> factoryOf(Algorithm algorithm) {
  1372             Objects.requireNonNull(algorithm);
  1397             Objects.requireNonNull(algorithm);
  1373             return RandomGeneratorFactory.factoryOf(algorithm.toString(), LeapableGenerator.class);
  1398             return RandomGeneratorFactory.factoryOf(algorithm.toString(), LeapableGenerator.class);
  1374         }
  1399         }
  1440         }
  1465         }
  1441 
  1466 
  1442     }
  1467     }
  1443 
  1468 
  1444     /**
  1469     /**
  1445      * This interface is designed to provide a common protocol for objects that generate sequences of
  1470      * This interface is designed to provide a common protocol for objects that generate sequences
  1446      * pseudorandom numbers (or Boolean values) and furthermore can easily <i>jump</i> to an arbitrarily
  1471      * of pseudorandom numbers (or Boolean values) and furthermore can easily <i>jump</i> to an
  1447      * specified distant point in the state cycle.
  1472      * arbitrarily specified distant point in the state cycle.
  1448      * <p>
  1473      * <p>
  1449      * Ideally, all {@link ArbitrarilyJumpableGenerator} objects produced by iterative jumping from a single
  1474      * Ideally, all {@link ArbitrarilyJumpableGenerator} objects produced by iterative jumping from
  1450      * original {@link ArbitrarilyJumpableGenerator} object are statistically independent of one another and
  1475      * a single original {@link ArbitrarilyJumpableGenerator} object are statistically independent
  1451      * individually uniform, provided that they do not traverse overlapping portions of the state cycle.
  1476      * of one another and individually uniform, provided that they do not traverse overlapping
  1452      *  In practice, one must settle for some approximation to independence and uniformity.  In
  1477      * portions of the state cycle. In practice, one must settle for some approximation to
  1453      * particular, a specific implementation may assume that each generator in a stream produced by the
  1478      * independence and uniformity.  In particular, a specific implementation may assume that each
  1454      * {@code jumps} method is used to produce a number of values no larger than the jump distance
  1479      * generator in a stream produced by the {@code jumps} method is used to produce a number of
  1455      * specified.  Implementors are advised to use algorithms whose period is at least 2<sup>127</sup>.
  1480      * values no larger than the jump distance specified.  Implementors are advised to use
  1456      * <p>
  1481      * algorithms whose period is at least 2<sup>127</sup>.
  1457      * For many applications, it suffices to jump forward by a power of two or some small multiple of a
  1482      * <p>
  1458      * power of two, but this power of two may not be representable as a {@code long} value.  To avoid
  1483      * For many applications, it suffices to jump forward by a power of two or some small multiple
  1459      * the use of {@link java.math.BigInteger} values as jump distances, {@code double} values are used
  1484      * of a power of two, but this power of two may not be representable as a {@code long} value.
  1460      * instead.
  1485      * To avoid the use of {@link java.math.BigInteger} values as jump distances, {@code double}
       
  1486      * values are used instead.
  1461      * <p>
  1487      * <p>
  1462      * Methods are provided to perform a single jump operation and also to produce a stream of
  1488      * Methods are provided to perform a single jump operation and also to produce a stream of
  1463      * generators produced from the original by iterative copying and jumping of internal state.  A
  1489      * generators produced from the original by iterative copying and jumping of internal state.  A
  1464      * typical strategy for a multithreaded application is to create a single {@link
  1490      * typical strategy for a multithreaded application is to create a single
  1465      * ArbitrarilyJumpableGenerator} object, call its {@code jumps} method exactly once, and then parcel out
  1491      * {@link ArbitrarilyJumpableGenerator} object, call its {@code jumps} method exactly once, and
  1466      * generators from the resulting stream, one to each thread.  However, each generator produced also
  1492      * then parcel out generators from the resulting stream, one to each thread.  However, each
  1467      * has type {@link ArbitrarilyJumpableGenerator}; with care, different jump distances can be used to
  1493      * generator produced also has type {@link ArbitrarilyJumpableGenerator}; with care, different
  1468      * traverse the entire state cycle in various ways.
  1494      * jump distances can be used to traverse the entire state cycle in various ways.
  1469      * <p>
  1495      * <p>
  1470      * An implementation of the {@link ArbitrarilyJumpableGenerator} interface must provide concrete
  1496      * An implementation of the {@link ArbitrarilyJumpableGenerator} interface must provide concrete
  1471      * definitions for the methods {@code nextInt()}, {@code nextLong}, {@code period()}, {@code
  1497      * definitions for the methods {@code nextInt()}, {@code nextLong}, {@code period()},
  1472      * copy()}, {@code jump(double)}, {@code defaultJumpDistance()}, and {@code defaultLeapDistance()}.
  1498      * {@code copy()}, {@code jump(double)}, {@code defaultJumpDistance()}, and
  1473      * Default implementations are provided for all other methods. Perhaps the most convenient way to
  1499      * {@code defaultLeapDistance()}. Default implementations are provided for all other methods.
  1474      * implement this interface is to extend the abstract class {@link ArbitrarilyJumpableGenerator}, which
  1500      * Perhaps the most convenient way to implement this interface is to extend the abstract class
  1475      * provides spliterator-based implementations of the methods {@code ints}, {@code longs}, {@code
  1501      * {@link ArbitrarilyJumpableGenerator}, which provides spliterator-based implementations of the
  1476      * doubles}, {@code rngs}, {@code jumps}, and {@code leaps}.
  1502      * methods {@code ints}, {@code longs}, {@code doubles}, {@code rngs}, {@code jumps}, and
  1477      * <p>
  1503      * {@code leaps}.
  1478      * Objects that implement {@link ArbitrarilyJumpableGenerator} are typically not cryptographically secure.
  1504      * <p>
  1479      * Consider instead using {@link java.security.SecureRandom} to get a cryptographically secure
  1505      * Objects that implement {@link ArbitrarilyJumpableGenerator} are typically not
  1480      * pseudo-random number generator for use by security-sensitive applications.
  1506      * cryptographically secure. Consider instead using {@link java.security.SecureRandom} to get a
       
  1507      * cryptographically secure pseudo-random number generator for use by security-sensitive
       
  1508      * applications.
  1481      *
  1509      *
  1482      * @since 14
  1510      * @since 14
  1483      */
  1511      */
  1484     public interface ArbitrarilyJumpableGenerator extends LeapableGenerator {
  1512     public interface ArbitrarilyJumpableGenerator extends LeapableGenerator {
  1485 
  1513 
  1513          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1541          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1514          * of {@link ArbitrarilyJumpableGenerator} that utilizes the {@code name} algorithm.
  1542          * of {@link ArbitrarilyJumpableGenerator} that utilizes the {@code name} algorithm.
  1515          *
  1543          *
  1516          * @param name  Name of random number generator algorithm
  1544          * @param name  Name of random number generator algorithm
  1517          *
  1545          *
  1518          * @return Factory of {@link ArbitrarilyJumpableGenerator}
  1546          * @return {@link RandomGeneratorFactory} of {@link ArbitrarilyJumpableGenerator}
  1519          */
  1547          */
  1520         public static RandomGeneratorFactory<ArbitrarilyJumpableGenerator> factoryOf(String name) {
  1548         public static RandomGeneratorFactory<ArbitrarilyJumpableGenerator> factoryOf(String name) {
  1521             Objects.requireNonNull(name);
  1549             Objects.requireNonNull(name);
  1522             return RandomGeneratorFactory.factoryOf(name, ArbitrarilyJumpableGenerator.class);
  1550             return RandomGeneratorFactory.factoryOf(name, ArbitrarilyJumpableGenerator.class);
  1523         }
  1551         }
  1526          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1554          * Returns a {@link RandomGeneratorFactory} that can produce instances
  1527          * of {@link ArbitrarilyJumpableGenerator} that utilizes the specified {@code algorithm}.
  1555          * of {@link ArbitrarilyJumpableGenerator} that utilizes the specified {@code algorithm}.
  1528          *
  1556          *
  1529          * @param algorithm  Random number generator algorithm
  1557          * @param algorithm  Random number generator algorithm
  1530          *
  1558          *
  1531          * @return Factory of {@link ArbitrarilyJumpableGenerator}
  1559          * @return {@link RandomGeneratorFactory} of {@link ArbitrarilyJumpableGenerator}
  1532          */
  1560          */
  1533         public static RandomGeneratorFactory<ArbitrarilyJumpableGenerator> factoryOf(Algorithm algorithm) {
  1561         public static RandomGeneratorFactory<ArbitrarilyJumpableGenerator> factoryOf(Algorithm algorithm) {
  1534             Objects.requireNonNull(algorithm);
  1562             Objects.requireNonNull(algorithm);
  1535             return RandomGeneratorFactory.factoryOf(algorithm.toString(), ArbitrarilyJumpableGenerator.class);
  1563             return RandomGeneratorFactory.factoryOf(algorithm.toString(), ArbitrarilyJumpableGenerator.class);
  1536         }
  1564         }
  1573          * that returned by method {@code defaultJumpDistance()}.
  1601          * that returned by method {@code defaultJumpDistance()}.
  1574          */
  1602          */
  1575         default void jump() { jump(defaultJumpDistance()); }
  1603         default void jump() { jump(defaultJumpDistance()); }
  1576 
  1604 
  1577         /**
  1605         /**
  1578          * Returns an effectively unlimited stream of new pseudorandom number generators, each of which
  1606          * Returns an effectively unlimited stream of new pseudorandom number generators, each of
  1579          * implements the {@link ArbitrarilyJumpableGenerator} interface, produced by jumping copies of this
  1607          * which implements the {@link ArbitrarilyJumpableGenerator} interface, produced by jumping
  1580          * generator by different integer multiples of the specified jump distance.
  1608          * copies of this generator by different integer multiples of the specified jump distance.
  1581          *
  1609          *
  1582          * @param distance a distance to jump forward within the state cycle
  1610          * @param distance a distance to jump forward within the state cycle
  1583          *
  1611          *
  1584          * @return a stream of objects that implement the {@link RandomGenerator} interface
  1612          * @return a stream of objects that implement the {@link RandomGenerator} interface
  1585          *
  1613          *
  1589             return Stream.generate(() -> copyAndJump(distance)).sequential();
  1617             return Stream.generate(() -> copyAndJump(distance)).sequential();
  1590         }
  1618         }
  1591 
  1619 
  1592         /**
  1620         /**
  1593          * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
  1621          * Returns a stream producing the given {@code streamSize} number of new pseudorandom number
  1594          * generators, each of which implements the {@link ArbitrarilyJumpableGenerator} interface, produced
  1622          * generators, each of which implements the {@link ArbitrarilyJumpableGenerator} interface,
  1595          * by jumping copies of this generator by different integer multiples of the specified jump
  1623          * produced by jumping copies of this generator by different integer multiples of the
  1596          * distance.
  1624          * specified jump distance.
  1597          *
  1625          *
  1598          * @param streamSize the number of generators to generate
  1626          * @param streamSize the number of generators to generate
  1599          * @param distance   a distance to jump forward within the state cycle
  1627          * @param distance   a distance to jump forward within the state cycle
  1600          *
  1628          *
  1601          * @return a stream of objects that implement the {@link RandomGenerator} interface
  1629          * @return a stream of objects that implement the {@link RandomGenerator} interface
  1606             return jumps(distance).limit(streamSize);
  1634             return jumps(distance).limit(streamSize);
  1607         }
  1635         }
  1608 
  1636 
  1609         /**
  1637         /**
  1610          * Alter the state of this pseudorandom number generator so as to jump forward a very large,
  1638          * Alter the state of this pseudorandom number generator so as to jump forward a very large,
  1611          * fixed distance (typically 2<sup>128</sup> or more) within its state cycle.  The distance used
  1639          * fixed distance (typically 2<sup>128</sup> or more) within its state cycle.  The distance
  1612          * is that returned by method {@code defaultJLeapDistance()}.
  1640          * used is that returned by method {@code defaultJLeapDistance()}.
  1613          */
  1641          */
  1614         default void leap() { jump(defaultLeapDistance()); }
  1642         default void leap() { jump(defaultLeapDistance()); }
  1615 
  1643 
  1616         /**
  1644         /**
  1617          * Copy this generator, jump this generator forward, then return the copy.
  1645          * Copy this generator, jump this generator forward, then return the copy.