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>−<i>w</i></sup>; if an explicit range is specified, then the chosen number is |
51 * 2<sup>−<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 |
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 |
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 |
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 |