Java Reference ยท Text, Formatting and Regex
The Random class
Reference for java.util.Random: seeded pseudo-random numbers, nextInt with bounds, doubles, Gaussian values, random streams, and why it is not for security.
java.util.Random is the classic pseudo-random number generator. It produces a sequence of numbers from a 48-bit internal state using a linear congruential formula. Two Random objects created with the same seed produce exactly the same sequence, which makes it useful for simulations, games and repeatable tests.
Because the sequence is fully determined by the seed, Random is predictable. Never use it for passwords, tokens or anything an attacker should not guess; use SecureRandom instead. In new code you can also program against the RandomGenerator interface that Random implements.
Random rng = new Random(42); int dice = rng.nextInt(6) + 1; // 1..6
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public class Random implements RandomGenerator, Serializable public Random() public Random(long seed)
| Member | Description |
|---|---|
int nextInt() |
Any int value, uniformly. |
int nextInt(int bound) |
A value from 0 (inclusive) to bound (exclusive). |
int nextInt(int origin, int bound) |
A value in a range (since Java 17, from RandomGenerator). |
long nextLong(), boolean nextBoolean() |
Random long or boolean. |
double nextDouble(), float nextFloat() |
A value from 0.0 (inclusive) to 1.0 (exclusive). |
double nextGaussian() |
Normally distributed value, mean 0, standard deviation 1. |
void nextBytes(byte[]) |
Fills the array with random bytes. |
ints(...), longs(...), doubles(...) |
Streams of random numbers, optionally sized and bounded. |
void setSeed(long) |
Restarts the sequence from a new seed. |
Throws: IllegalArgumentException when a bound is zero or negative, or when an origin is not less than the bound.
Usage notes
- The no-argument constructor picks a seed that differs for every instance, so unseeded generators give different sequences. Pass a fixed seed when you need reproducible output, for example in tests.
- The exact sequence for a given seed is specified, so
new Random(42)gives the same values on every JVM and Java version for the long-standing methods. The newer range methods may use different algorithms than the older ones. - Thread safety. A
Randomcan be shared between threads, but threads contend on one atomic seed. For concurrent code useThreadLocalRandom. - Not secure. After seeing a few outputs an attacker can compute the seed and every later value.
- Common mistake: creating a new
Randomfor every call (for example inside a loop with the clock as seed). Create one and reuse it. - Shuffling: pass your generator to
Collections.shuffle(list, rng)to make a shuffle reproducible. - Modulo bias: use
nextInt(bound), notMath.abs(nextInt()) % bound; the second form is biased and fails forInteger.MIN_VALUE.
Related
RandomGenerator,ThreadLocalRandom,SecureRandom- Tutorial: Random numbers