Java : Random with Examples

Random (Java SE 17 & JDK 17) API Examples.
You will find code examples on most Random methods.


Summary

An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 248.

Class diagram

final var random = new Random();

IntStream.range(0, 10).forEach(i -> {
    // Generates random numbers from 0 to 999.
    final var value = random.nextInt(1000);
    System.out.println(value);
});

// Result
// ↓
//842
//947
//117
//930
//586
//634
//48
//754
//615
//538

See also below.

Choosing a Random Number Generator Algorithm
...
Random (LCG) is the weakest of the available algorithms, and it is recommended that users migrate to newer algorithms. If an application requires a random number generator algorithm that is cryptographically secure, then it should continue to use an instance of the class SecureRandom.


Constructors

Random ()

Creates a new random number generator.

final var randomA = new Random();
randomA.ints(5).forEach(System.out::println);

// Result
// ↓
//-841255616
//-429008669
//1410901662
//-1564542540
//580020011

final var randomB = new Random();
randomB.ints(5).forEach(System.out::println);

// Result
// ↓
//545903664
//-1228814490
//1828005830
//580827886
//-1850876258

Random (long seed)

Creates a new random number generator using a single long seed.

final var seed = 1L;

final var randomA = new Random(seed);
randomA.ints(5).forEach(System.out::println);

// Result
// ↓
//-1155869325
//431529176
//1761283695
//1749940626
//892128508

final var randomB = new Random(seed);
randomB.ints(5).forEach(System.out::println);

// Result
// ↓
//-1155869325
//431529176
//1761283695
//1749940626
//892128508

Methods

DoubleStream doubles ()

Returns an effectively unlimited stream of pseudorandom double values, each between zero (inclusive) and one (exclusive).

final var random = new Random();

final var stream = random.doubles();
stream.limit(5).forEach(System.out::println);

// Result
// ↓
//0.9933971955304746
//0.1483121246786262
//0.08813398340371925
//0.2371977856503633
//0.4369634515475669

DoubleStream doubles (double randomNumberOrigin, double randomNumberBound)

Returns an effectively unlimited stream of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

final var random = new Random();

final var stream = random.doubles(-2.0, 2.0);
stream.limit(10).forEach(System.out::println);

// Result
// ↓
//0.46669107214800976
//1.823982612104178
//-1.0023020499122088
//1.557809549013963
//-1.8460889354201009
//1.580609013740684
//-1.9844530745601592
//1.6292569996897277
//0.8971618701335204
//-0.2060437417207921

DoubleStream doubles (long streamSize)

Returns a stream producing the given streamSize number of pseudorandom double values, each between zero (inclusive) and one (exclusive).

final var random = new Random();

final var stream = random.doubles(5);
stream.forEach(System.out::println);

// Result
// ↓
//0.6069609094487831
//0.1041672960050839
//0.9408040476538043
//0.3550482245920191
//0.01859977291581849

DoubleStream doubles (long streamSize, double randomNumberOrigin, double randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom double values, each conforming to the given origin (inclusive) and bound (exclusive).

final var random = new Random();

final var stream = random.doubles(10, -2.0, 2.0);
stream.forEach(System.out::println);

// Result
// ↓
//0.12394741420391142
//1.715446838272169
//-0.7602225000178544
//1.396642879771861
//-0.12330350387046352
//1.6982792721646134
//-0.22836900988449882
//0.4818543322048803
//1.537344284465561
//-0.5734181028132959

IntStream ints ()

Returns an effectively unlimited stream of pseudorandom int values.

final var random = new Random();

final var stream = random.ints();
stream.limit(5).forEach(System.out::println);

// Result
// ↓
//-387527003
//1501625326
//1043996810
//248435097
//-1490084866

IntStream ints (int randomNumberOrigin, int randomNumberBound)

Returns an effectively unlimited stream of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).

final var random = new Random();

final var stream = random.ints(-20, 20);
stream.limit(10).forEach(System.out::println);

// Result
// ↓
//-11
//10
//-16
//-11
//-5
//5
//-4
//16
//-4
//-20

IntStream ints (long streamSize)

Returns a stream producing the given streamSize number of pseudorandom int values.

final var random = new Random();

final var stream = random.ints(5);
stream.forEach(System.out::println);

// Result
// ↓
//1274439095
//-555311190
//-560506878
//1556824651
//1395678367

IntStream ints (long streamSize, int randomNumberOrigin, int randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom int values, each conforming to the given origin (inclusive) and bound (exclusive).

final var random = new Random();

final var stream = random.ints(10, -20, 20);
stream.forEach(System.out::println);

// Result
// ↓
//4
//18
//-14
//-19
//-7
//4
//-8
//14
//-20
//-7

LongStream longs ()

Returns an effectively unlimited stream of pseudorandom long values.

final var random = new Random();

final var stream = random.longs();
stream.limit(5).forEach(System.out::println);

// Result
// ↓
//-2593912032996472717
//6165299436042818497
//7459575653066080807
//1821618543822402933
//-7464300688093417511

LongStream longs (long streamSize)

Returns a stream producing the given streamSize number of pseudorandom long values.

final var random = new Random();

final var stream = random.longs(5);
stream.forEach(System.out::println);

// Result
// ↓
//-8771583458493279999
//4087852178343636982
//-712956439607902834
//-5729178964099416988
//4678616029229920469

LongStream longs (long randomNumberOrigin, long randomNumberBound)

Returns an effectively unlimited stream of pseudorandom long values, each conforming to the given origin (inclusive) and bound (exclusive).

final var random = new Random();

final var stream = random.longs(-20, 20);
stream.limit(10).forEach(System.out::println);

// Result
// ↓
//-11
//-7
//18
//-13
//-17
//-5
//3
//-20
//13
//0

LongStream longs (long streamSize, long randomNumberOrigin, long randomNumberBound)

Returns a stream producing the given streamSize number of pseudorandom long, each conforming to the given origin (inclusive) and bound (exclusive).

final var random = new Random();

final var stream = random.longs(10, -20, 20);
stream.forEach(System.out::println);

// Result
// ↓
//19
//-9
//-11
//2
//-10
//-4
//16
//-6
//2
//11

protected int next (int bits)

Generates the next pseudorandom number.

I think it's rare to create a subclass of Random. Therefore, the code example is omitted.

boolean nextBoolean ()

Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.

final var random = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var value = random.nextBoolean();
    System.out.println(value);
});

// Result
// ↓
//true
//true
//false
//true
//false

void nextBytes (byte[] bytes)

Generates random bytes and places them into a user-supplied byte array.

final var random = new Random();

final var bytes = new byte[10];
random.nextBytes(bytes);

// [86, -2, -56, -114, 56, -114, 13, 55, 104, 29]
System.out.println(Arrays.toString(bytes));

random.nextBytes(bytes);

// [-67, 44, 80, -39, 9, 38, 60, 49, 67, 127]
System.out.println(Arrays.toString(bytes));

double nextDouble ()

Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

final var random = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var value = random.nextDouble();
    System.out.println(value);
});

// Result
// ↓
//0.612952216727897
//0.7367017962886943
//0.3850142745997098
//0.6757458526953953
//0.5978931541933749

float nextFloat ()

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

final var random = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var value = random.nextFloat();
    System.out.println(value);
});

// Result
// ↓
//0.3875636
//0.9419651
//0.35063696
//0.75571376
//0.35421652

double nextGaussian ()

Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

final var random = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var value = random.nextGaussian();
    System.out.println(value);
});

// Result
// ↓
//-0.982573556204939
//0.6108520447839247
//1.4606584096086386
//0.5642834099747783
//-0.2853352253670542

int nextInt ()

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

final var random = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var value = random.nextInt();
    System.out.println(value);
});

// Result
// ↓
//253911642
//569627001
//-299685362
//1499873563
//-757847850

int nextInt (int bound)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

final var random = new Random();

IntStream.range(0, 10).forEach(i -> {
    final var value = random.nextInt(1000);
    System.out.println(value);
});

// Result
// ↓
//842
//947
//117
//930
//586
//634
//48
//754
//615
//538

long nextLong ()

Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

final var random = new Random();

IntStream.range(0, 5).forEach(i -> {
    final var value = random.nextLong();
    System.out.println(value);
});

// Result
// ↓
//622295099449075800
//-4149873296563324808
//-8567274942269278677
//5245993659038234640
//4681780131712481383

void setSeed (long seed)

Sets the seed of this random number generator using a single long seed.

final var random = new Random(1);
random.ints(5).forEach(System.out::println);

// Result
// ↓
//-1155869325
//431529176
//1761283695
//1749940626
//892128508

random.setSeed(1);
random.ints(5).forEach(System.out::println);

// Result
// ↓
//-1155869325
//431529176
//1761283695
//1749940626
//892128508

Methods declared in RandomGenerator

isDeprecated, nextDouble, nextDouble, nextExponential, nextFloat, nextFloat, nextGaussian, nextInt, nextLong, nextLong

Please see the link below.


Related posts

To top of page