Quick revision
The short answer
java.util.Random is a thread-safe but predictable linear congruential generator whose shared seed becomes a point of contention across threads. ThreadLocalRandom gives each thread its own fast generator, SplittableRandom can split into independent generators for parallel work, and SecureRandom is the cryptographically strong one. Java 17 added the RandomGenerator interface and RandomGeneratorFactory so we can choose algorithms such as L64X128MixRandom by name. For a session token we use SecureRandom; for a simulation on many threads we use a splittable or jumpable algorithm, giving each task its own generator.
What to remember
In this answer, we saw that Random is a thread-safe but predictable generator whose single seed causes contention, ThreadLocalRandom removes the contention with per-thread state, SplittableRandom and the Java 17 splittable and jumpable algorithms give independent generators for parallel and reproducible work, and SecureRandom is the only choice for secrets. For a session token we use one shared SecureRandom with at least 128 bits; for a multi-threaded simulation we split a seeded generator such as L64X128MixRandom into one per task.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
java.util.Random is a thread-safe but predictable linear congruential generator whose shared seed becomes a point of contention across threads. ThreadLocalRandom gives each thread its own fast generator, SplittableRandom can split into independent generators for parallel work, and SecureRandom is the cryptographically strong one. Java 17 added the RandomGenerator interface and RandomGeneratorFactory so we can choose algorithms such as L64X128MixRandom by name. For a session token we use SecureRandom; for a simulation on many threads we use a splittable or jumpable algorithm, giving each task its own generator.
Why this question is asked
Java has five ways to get random numbers, and choosing the wrong one causes two very different failures: a security hole when tokens can be predicted, or a slow, unrepeatable simulation when threads fight over one generator. Interviewers ask this to check that we know what each class is for, not just that it exists. In this answer, we will recap what a pseudorandom generator is, go through each option and what it costs, look at the Java 17 interface that ties them together, and then choose a generator for the two cases in the question.
1A Quick Recap of Pseudorandom Generators
Computers usually do not produce truly random numbers. A pseudorandom number generator, or PRNG, starts from a seed, a starting number, and applies a fixed formula to produce each next value. The output looks random and passes statistical tests, but the same seed always gives the same sequence. That is useful for simulations and tests, which we want to repeat, and dangerous for secrets, because anyone who learns the seed or internal state can compute every future value.
A cryptographically secure generator, or CSPRNG, is designed so that seeing its output does not help anyone predict the next value. It is seeded from the operating system's entropy, unpredictable data gathered from hardware events, and its algorithm resists reconstruction. It is slower, and it is the only kind we may use for secrets.
| Option | Thread-safe? | Predictable? | Best for |
|---|---|---|---|
java.util.Random | yes, via CAS on one seed | yes, from a few outputs | legacy code, seeded tests |
ThreadLocalRandom | one per thread | yes | general use in concurrent code |
SplittableRandom | no, split per task | yes | parallel and fork-join work |
SecureRandom | yes | no (CSPRNG) | tokens, keys, salts, nonces |
RandomGenerator (17+) | depends on algorithm | depends | choosing algorithms by name |
SecureRandom is suitable for anything an attacker should not guess.2Random and Its Shared Seed
java.util.Random is a linear congruential generator: its 48-bit state is multiplied by a constant, a constant is added, and the result is the next state. The state lives in an AtomicLong, and every call updates it with a compare-and-set loop, so Random is thread-safe. But there is a catch: when many threads share one instance, they all compete to update that one value, and failed compare-and-set attempts retry, so throughput drops as threads are added. Math.random() uses one shared Random internally, so it has the same contention.
Notice the fixed seed: two Random objects with seed 42 produce identical sequences. That is exactly what we want in a reproducible test and exactly what we must avoid for secrets.
ThreadLocalRandom, added in Java 7, solves contention by keeping the generator state per thread. We always call ThreadLocalRandom.current() at the point of use. Storing the returned object in a field and sharing it defeats the design, because the state belongs to the thread that called current().
3SplittableRandom and Parallel Work
SplittableRandom, added in Java 8, is a fast generator built for divide-and-conquer computations. Its split() method creates a new generator whose sequence is statistically independent of the parent's. A fork-join task splits a generator for each subtask, and each subtask uses its own instance without any sharing. The instance itself is not thread-safe, which is fine, because it is never shared. Splitting from a fixed root seed also makes a parallel computation repeatable, which ThreadLocalRandom cannot offer because its per-thread seeds are not under our control.
4SecureRandom for Anything Secret
SecureRandom is the CSPRNG. On Linux the default new SecureRandom() is the NativePRNG algorithm, which draws from the operating system's generator, and DRBG is the standard algorithm available everywhere. It is thread-safe, so one shared instance is fine.
We use it for session tokens, password reset tokens, API keys, salts and nonces. java.util.Random must never be used for these: its 48-bit state can be recovered from a couple of outputs, after which an attacker can generate the same tokens the server will issue next.
5The RandomGenerator Interface From Java 17
Java 17, through JEP 356, gave all these classes a common interface, java.util.random.RandomGenerator, and added a family of new algorithms called LXM, plus Xoshiro and Xoroshiro variants. Code can now depend on the interface and pick an algorithm by name, which makes swapping algorithms a one-line change.
The sub-interfaces describe what an algorithm can do. A SplittableGenerator, like the LXM family, can split into independent children, as SplittableRandom does. A JumpableGenerator, like Xoshiro256PlusPlus, can jump far ahead in its sequence, so each worker gets a non-overlapping stretch of the same stream. RandomGeneratorFactory lists the available algorithms with their properties, such as period and state size, and creates instances with a seed.
6Choosing for a Token and for a Simulation
For a session token the choice is fixed: SecureRandom, one shared instance, at least 128 bits of output, encoded with URL-safe Base64. Speed is irrelevant compared with predictability.
For a simulation running on many threads we want three things: no contention, good statistical quality over very long runs, and reproducibility. So we create one splittable generator from a recorded seed, such as L64X128MixRandom, split it into one generator per task, and pass each task its own. If the simulation does not need to be repeatable, ThreadLocalRandom.current() is the simple, contention-free choice. What we avoid is one shared Random, which would be both slow and, because thread scheduling changes the order of calls, not reproducible.
- Must an attacker be unable to guess it?YesSecureRandomNoContinue
- Must the run be reproducible?YesSeeded splittable generator, split per taskNoContinue
- Used from many threads?YesThreadLocalRandom.current()NoAny RandomGenerator, e.g. getDefault()
3 steps in order. Play them, or step through them one at a time.
Security first, then reproducibility, then convenience.
7Follow-up questions
Why is new Random(System.currentTimeMillis()) a bad idea for tokens?
Because both the seed and the algorithm are guessable. An attacker who knows roughly when the server started or when the token was made only needs to try a few thousand millisecond values, and for each one the sequence is fully determined. Even without the seed, Random's 48-bit state can be reconstructed from two consecutive outputs. Tokens need SecureRandom, which is seeded from the operating system and resists prediction.
Is SecureRandom slow enough to matter?
Usually not. Generating a few hundred thousand tokens per second is within reach of one shared instance on a modern machine. If profiling shows contention, the usual fix is a few instances spread across threads, not a weaker algorithm.
Can I make ThreadLocalRandom reproducible in tests?
No, its seeds cannot be set. Code that needs repeatable randomness in tests should accept a RandomGenerator as a parameter or bean, so production passes a default generator and the test passes a seeded one.
8Conclusion
In this answer, we saw that Random is a thread-safe but predictable generator whose single seed causes contention, ThreadLocalRandom removes the contention with per-thread state, SplittableRandom and the Java 17 splittable and jumpable algorithms give independent generators for parallel and reproducible work, and SecureRandom is the only choice for secrets. For a session token we use one shared SecureRandom with at least 128 bits; for a multi-threaded simulation we split a seeded generator such as L64X128MixRandom into one per task.
Open with
I think of it as two questions: can someone guess it, and does it need to be fast and repeatable across threads. java.util.Random is a linear congruential generator with 48 bits of state in an AtomicLong.
Then, if they want more
It is thread-safe, but threads sharing one instance contend on that seed, and it is predictable: two outputs are enough to reconstruct the state. ThreadLocalRandom, from Java 7, keeps the state per thread, so there is no contention; you call ThreadLocalRandom.current() where you use it and never share the instance.
SplittableRandom, from Java 8, is built for fork-join work: each task splits off its own independent generator, and from a fixed seed the whole computation is repeatable. SecureRandom is the cryptographically strong one, seeded from the operating system, and it is the only option for tokens, keys and salts.
Java 17 added the RandomGenerator interface and RandomGeneratorFactory, so we can choose algorithms by name, like L64X128MixRandom, which is splittable, or Xoshiro256PlusPlus, which is jumpable. So for a session token I would use one shared SecureRandom, at least 128 bits, URL-safe Base64.
For a simulation on many threads, I would create a splittable generator from a recorded seed, split it into one per task, and pass each task its own, which gives no contention and reproducible results.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 7, Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare java.util.Random, ThreadLocalRandom, SplittableRandom, SecureRandom and the...": https://scrivio-interview-library.netlify.app/java/types-and-values/68c75200b2/