random: Random vs ThreadLocalRandom vs SecureRandom vs RandomGenerator module (seeds and a 2-output predictor, ranges and measured modulo bias, ThreadLocalRandom limits, SecureRandom algorithms and setSeed behaviour, the 13 java.util.random algorithms with split/jump, indicative timings), 16 contract tests
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6F5pq1M5ni1bNw8gr
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
|
||||
== Same seed, same sequence ==
|
||||
Random(42) #1: 30 63 48 84 70
|
||||
Random(42) #2: 30 63 48 84 70
|
||||
|
||||
== No seed: every instance starts somewhere different ==
|
||||
new Random() #1: 94 42 76
|
||||
new Random() #2: 86 37 29
|
||||
(these two lines differ on every run)
|
||||
|
||||
== setSeed rewinds an existing instance ==
|
||||
first draw before setSeed(7): 236, first draw after: 236
|
||||
|
||||
== Two outputs of an unseeded Random predict the third ==
|
||||
seen output 1 : 1471292627
|
||||
seen output 2 : 160425440
|
||||
predicted 3 : 239866061
|
||||
actual 3 : 239866061
|
||||
prediction correct: true
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
== The right calls (upper bound is always exclusive) ==
|
||||
nextInt(10) 0..9 : 5
|
||||
nextInt(1, 7) 1..6 : 5
|
||||
nextInt(-5, 6) -5..5 : 3
|
||||
nextDouble() [0,1) : 0.4074398012118764
|
||||
nextDouble(2, 3) [2,3) : 2.036235382150368
|
||||
ints(5, 1, 7) : [5, 5, 5, 2, 2]
|
||||
|
||||
== Bad arguments fail loudly ==
|
||||
nextInt(0) -> bound must be positive
|
||||
nextInt(5, 5) -> bound must be greater than origin
|
||||
|
||||
== Trap 1: nextInt() % n can be negative ==
|
||||
1000 draws of nextInt() % 6, negative results: 397
|
||||
|
||||
== Trap 2: Math.abs(Integer.MIN_VALUE) is still negative ==
|
||||
Math.abs(Integer.MIN_VALUE) = -2147483648
|
||||
Math.abs(Integer.MIN_VALUE) % 10 = -8
|
||||
(nextInt() hits MIN_VALUE once in 2^32 calls, so tests never catch it)
|
||||
|
||||
== Trap 3: modulo bias ==
|
||||
3 random bits (0..7) % 3 -> counts of 0,1,2: [3, 3, 2]
|
||||
bound = 1,500,000,000, results below 647,483,648
|
||||
expected share if uniform : 0.4317
|
||||
(nextInt() >>> 1) % bound : 0.6031
|
||||
nextInt(bound) : 0.4315
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
== The one correct way to call it ==
|
||||
ThreadLocalRandom.current().nextInt(1, 7) -> in 1..6: true
|
||||
|
||||
== It cannot be seeded ==
|
||||
setSeed(42) -> UnsupportedOperationException, message: null
|
||||
|
||||
== Each thread has its own state ==
|
||||
4 threads, distinct first values: 4 of 4
|
||||
|
||||
== Works the same on virtual threads ==
|
||||
virtual thread rolled a die: true
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
== What new SecureRandom() gives you on this machine ==
|
||||
algorithm : NativePRNG
|
||||
provider : SUN
|
||||
|
||||
== getInstanceStrong() ==
|
||||
algorithm : NativePRNGBlocking
|
||||
securerandom.strongAlgorithms = NativePRNGBlocking:SUN,DRBG:SUN
|
||||
|
||||
== SecureRandom algorithms registered in this JDK ==
|
||||
DRBG (SUN)
|
||||
NativePRNG (SUN)
|
||||
NativePRNGBlocking (SUN)
|
||||
NativePRNGNonBlocking (SUN)
|
||||
SHA1PRNG (SUN)
|
||||
|
||||
== A session token: 32 random bytes = 256 bits ==
|
||||
length in characters: 43 (value differs on every run)
|
||||
|
||||
== Seeding: SHA1PRNG is deterministic, the default is not ==
|
||||
SHA1PRNG, same seed, same bytes: true
|
||||
NativePRNG, same seed, same bytes: false (setSeed only adds entropy; it does not make the output repeatable)
|
||||
|
||||
== It is also a RandomGenerator, so the range methods work ==
|
||||
nextInt(1, 7) in 1..6: true
|
||||
@@ -0,0 +1,39 @@
|
||||
|
||||
== The old classes already implement RandomGenerator ==
|
||||
Random -> rollDie in 1..6: true
|
||||
SecureRandom -> rollDie in 1..6: true
|
||||
SplittableRandom -> rollDie in 1..6: true
|
||||
ThreadLocalRandom -> rollDie in 1..6: true
|
||||
|
||||
== The default generator ==
|
||||
RandomGenerator.getDefault() : jdk.internal.random.L32X64MixRandom
|
||||
RandomGeneratorFactory.getDefault() : L32X64MixRandom
|
||||
|
||||
== Every algorithm this JDK ships (name, group, state bits, jumpable, splittable) ==
|
||||
L128X1024MixRandom LXM 1152 bits jumpable=false splittable=true
|
||||
L128X128MixRandom LXM 256 bits jumpable=false splittable=true
|
||||
L128X256MixRandom LXM 384 bits jumpable=false splittable=true
|
||||
L32X64MixRandom LXM 96 bits jumpable=false splittable=true
|
||||
L64X1024MixRandom LXM 1088 bits jumpable=false splittable=true
|
||||
L64X128MixRandom LXM 192 bits jumpable=false splittable=true
|
||||
L64X128StarStarRandom LXM 192 bits jumpable=false splittable=true
|
||||
L64X256MixRandom LXM 320 bits jumpable=false splittable=true
|
||||
Random Legacy 48 bits jumpable=false splittable=false
|
||||
SecureRandom Legacy 2147483647 bits jumpable=false splittable=false
|
||||
SplittableRandom Legacy 64 bits jumpable=false splittable=true
|
||||
Xoroshiro128PlusPlus Xoroshiro 128 bits jumpable=true splittable=false
|
||||
Xoshiro256PlusPlus Xoshiro 256 bits jumpable=true splittable=false
|
||||
|
||||
== Pick an algorithm by name, and seed it ==
|
||||
Xoshiro256PlusPlus seed 42 #1: 600 767 404
|
||||
Xoshiro256PlusPlus seed 42 #2: 600 767 404
|
||||
RandomGenerator.of("L64X128MixRandom") -> L64X128MixRandom
|
||||
|
||||
== Splittable: independent generators for parallel work ==
|
||||
first draw of 4 split children: 952 921 97 912
|
||||
|
||||
== Jumpable: provably non-overlapping streams ==
|
||||
jump distance : 3.402823669209385E38
|
||||
leap distance : 6.277101735386681E57
|
||||
original next: 600
|
||||
after jump next: 545
|
||||
@@ -0,0 +1,15 @@
|
||||
cores available: 2
|
||||
|
||||
== One thread: nanoseconds per nextInt(), 20000000 calls ==
|
||||
Random 8.5 ns/op
|
||||
SplittableRandom 0.7 ns/op
|
||||
ThreadLocalRandom 2.1 ns/op
|
||||
L32X64MixRandom (default) 2.9 ns/op
|
||||
Xoshiro256PlusPlus 2.7 ns/op
|
||||
SecureRandom 90.6 ns/op
|
||||
|
||||
== 4 threads sharing ONE instance, 5000000 calls each (wall time, ms) ==
|
||||
Random (shared) 987 ms
|
||||
SecureRandom (shared) 7329 ms
|
||||
ThreadLocalRandom.current() 37 ms
|
||||
checksum (ignore): -28432667108051
|
||||
@@ -0,0 +1,3 @@
|
||||
[INFO] Tests run: 16, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.159 s -- in com.ankurm.random.RandomContractTest
|
||||
[INFO] Tests run: 16, Failures: 0, Errors: 0, Skipped: 0
|
||||
[INFO] BUILD SUCCESS
|
||||
Reference in New Issue
Block a user