Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6F5pq1M5ni1bNw8gr
28 lines
951 B
Plaintext
28 lines
951 B
Plaintext
|
|
== 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
|