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:
Claude
2026-11-19 22:50:44 +05:30
parent 4511cc6e6b
commit 59a5435825
22 changed files with 780 additions and 0 deletions
+27
View File
@@ -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