Files

38 lines
1.6 KiB
Markdown

# random
Companion code for the ankurm.com post *"Random vs ThreadLocalRandom vs SecureRandom vs RandomGenerator in Java."*
Module in `java-core-examples`, the Java-core series. All explanation lives in the post; this module holds the code and the captured output.
## Versions this was built and tested against
| Component | Version | Notes |
|---|---|---|
| JDK | 25.0.5.1 (Temurin, LTS) | `java.util.random` (JEP 356) arrived in JDK 17; `ThreadLocalRandom` in Java 7. |
| JUnit Jupiter | 5.11.0 | |
| Maven | 3.9.11 | |
## Quickstart
```bash
export JAVA_HOME=/path/to/jdk-25
mvn compile
scripts/run.sh SeedDemo # one demo
scripts/run-all.sh # regenerate every file in output/
```
## What's in here
| File | What it shows | Output |
|---|---|---|
| `SeedDemo.java` + `Predictor.java` | Same seed, same sequence; `setSeed`; two `nextInt()` results predict the third. | `output/01` |
| `RangeDemo.java` | `nextInt(origin, bound)`, the error messages, `% n` going negative, `Math.abs(MIN_VALUE)`, modulo bias measured. | `output/02` |
| `ThreadLocalDemo.java` | `current()`, `setSeed` refused, one stream per thread, virtual threads. | `output/03` |
| `SecureRandomDemo.java` | Which algorithm you get, `getInstanceStrong()`, tokens, what `setSeed` does. | `output/04` |
| `RandomGeneratorDemo.java` | The interface, the default, the 13 algorithms in JDK 25, split and jump. | `output/05` |
| `BenchmarkDemo.java` | Indicative timings (not JMH). Drifts between runs. | `output/06` |
| `RandomContractTest.java` | Pins every claim above as an assertion, 16/16 passing. | `output/07` |
## License
MIT - see the [repo-wide LICENSE](../LICENSE).