Java : RandomGenerator with Examples

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


Summary

The RandomGenerator interface is designed to provide a common protocol for objects that generate random or (more typically) pseudorandom sequences of numbers (or Boolean values).

Class diagram

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//-631306984
//360282856
//1268279487
//345831916
//-296659039

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.


Methods

default DoubleStream doubles ()

Returns an effectively unlimited stream of pseudorandomly chosen double values.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//0.16656199123222215
//0.6218217049912959
//0.19600772989497395
//0.555628646104419
//0.7930447771788491

default DoubleStream doubles (double randomNumberOrigin, double randomNumberBound)

Returns an effectively unlimited stream of pseudorandomly chosen double values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//2.0006280523771913
//-2.9668945293573588
//-1.977972961932046
//1.4591087127881943
//1.1764776163707467
//0.324156307444031
//0.11521933606309265
//0.5809518129659113
//-1.5043107539384146
//1.5041637745383651

default DoubleStream doubles (long streamSize)

Returns a stream producing the given streamSize number of pseudorandomly chosen double values.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//0.8334380086898268
//0.03037022005663459
//0.10880306626740466
//0.32559504023785624
//0.5128375942419151

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

Returns a stream producing the given streamSize number of pseudorandomly chosen double values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//-2.0006280523452276
//2.496709103408133
//2.7277152710430457
//2.063650144667279
//0.8035359256505972
//-1.5947828941532585
//-0.4565496604566852
//-0.8288139724521377
//2.3173434035873637
//-1.6395531086397552

static RandomGenerator getDefault ()

Returns a RandomGenerator meeting the minimal requirement of having an algorithm whose state bits are greater than or equal 64.

final var random = RandomGenerator.getDefault();
System.out.println(random.getClass().getName()); // jdk.random.L32X64MixRandom

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

// Result
// ↓
//715378305
//101370956
//694682166
//-1355452562
//1273544944

default IntStream ints ()

Returns an effectively unlimited stream of pseudorandomly chosen int values.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//715378305
//-825023944
//-473389675
//234171438
//-1489011721

default IntStream ints (int randomNumberOrigin, int randomNumberBound)

Returns an effectively unlimited stream of pseudorandomly chosen int values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//152
//207
//-685
//-549
//700
//593
//-814
//2
//646
//-774

default IntStream ints (long streamSize)

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

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//715378305
//-1250363547
//1871257264
//784792419
//1413829192

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

Returns a stream producing the given streamSize number of pseudorandomly chosen int values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//152
//-644
//497
//-914
//961
//887
//930
//-423
//775
//476

default boolean isDeprecated ()

Return true if the implementation of RandomGenerator (algorithm) has been marked for deprecation.

final var random = RandomGenerator.getDefault();
System.out.println(random.isDeprecated()); // false
final RandomGenerator random = new Random();
System.out.println(random.isDeprecated()); // false

default LongStream longs ()

Returns an effectively unlimited stream of pseudorandomly chosen long values.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//3072526424993141147
//-1228477046872454351
//-6421471344833399471
//6602871497182012087
//2316779264487820838

default LongStream longs (long streamSize)

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

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//3072526424836303138
//-1694446413389799187
//-1584792343893839579
//5466113612786326664
//-769135330838364015

default LongStream longs (long randomNumberOrigin, long randomNumberBound)

Returns an effectively unlimited stream of pseudorandomly chosen long values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//-300
//977
//-611
//-503
//-437
//-99
//713
//-955
//856
//73

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

Returns a stream producing the given streamSize number of pseudorandomly chosen long values, where each value is between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//-150
//-913
//376
//-870
//-399
//249
//-76
//-299
//-890
//-404

default boolean nextBoolean ()

Returns a pseudorandomly chosen boolean value.

final var random = RandomGenerator.getDefault();

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

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

default void nextBytes (byte[] bytes)

Fills a user-supplied byte array with generated byte values pseudorandomly chosen uniformly from the range of values between -128 (inclusive) and 127 (inclusive).

final var random = RandomGenerator.getDefault();

final var bytes = new byte[5];
System.out.println(Arrays.toString(bytes)); // [0, 0, 0, 0, 0]

random.nextBytes(bytes);
System.out.println(Arrays.toString(bytes)); // [-96, 60, 2, 14, -127]

random.nextBytes(bytes);
System.out.println(Arrays.toString(bytes)); // [84, -94, 75, -5, -27]

default double nextDouble ()

Returns a pseudorandom double value between zero (inclusive) and one (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//0.16656199121570903
//0.23367465009528576
//0.9284442490070819
//0.36413039663241964
//0.09374887895945716

default double nextDouble (double bound)

Returns a pseudorandomly chosen double value between zero (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//2.500314026063205
//1.8220919846300938
//1.9564880970747494
//0.1595970117169947
//1.3449282414104538
//1.7025848107213548
//1.4033170314701464
//2.473710659828541
//1.237554388744942
//0.3273167095734788

default double nextDouble (double origin, double bound)

Returns a pseudorandomly chosen double value between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//2.0006280525762694
//1.0798768391242595
//-0.5062689333449741
//-0.6804432211100737
//1.319150255507057
//0.3724747688240164
//2.11873513987363
//1.745673342239547
//2.412951541889652
//-2.6840469600622776

default double nextExponential ()

Returns a nonnegative double value pseudorandomly chosen from an exponential distribution whose mean is 1.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//1.4853544341415674
//0.2791056072286121
//1.0625803161169822
//1.837581139676294
//1.584304548356971
//1.2168826889961073
//2.350141721243219
//2.0809004851879367
//2.518312711772013
//0.17643183637445614

default float nextFloat ()

Returns a pseudorandom float value between zero (inclusive) and one (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//0.16656196
//0.60083836
//0.21343786
//0.7307061
//0.7694186

default float nextFloat (float bound)

Returns a pseudorandomly chosen float value between zero (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//0.49968588
//2.9137702
//1.3287137
//1.2837255
//0.56128746
//1.7413721
//2.194965
//2.5983307
//1.48546
//0.9885877

default float nextFloat (float origin, float bound)

Returns a pseudorandomly chosen float value between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//-2.0006282
//-2.3810701
//0.201478
//-0.76499224
//-1.1443824
//1.2276878
//-1.6110066
//-1.8389
//-1.0752397
//-1.252554

default double nextGaussian ()

Returns a double value pseudorandomly chosen from a Gaussian (normal) distribution whose mean is 0 and whose standard deviation is 1.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//0.39882238122220826
//0.304753801685565
//1.3948447451282993
//-0.7410088182528803
//1.4326498037311102
//-0.34115612322362765
//-0.08134889265351929
//-1.1272254737999339
//-0.1855275934958847
//0.20835530787127568

default double nextGaussian (double mean, double stddev)

Returns a double value pseudorandomly chosen from a Gaussian (normal) distribution with a mean and standard deviation specified by the arguments.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//1.8436990506325182
//-0.2532608050079741
//2.03751904185916
//2.0035285211930285
//3.7245125081288464
//1.565431386311888
//1.4723064254841673
//2.396063711627094
//-1.9759561643517785
//3.3377298846768513

default int nextInt ()

Returns a pseudorandomly chosen int value.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//715378305
//-1167476346
//-928563181
//257107968
//-870146779

default int nextInt (int bound)

Returns a pseudorandomly chosen int value between zero (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//152
//419
//93
//875
//521
//662
//829
//476
//727
//985

default int nextInt (int origin, int bound)

Returns a pseudorandomly chosen int value between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//152
//385
//2
//796
//-239
//-84
//223
//-690
//-408
//-194

long nextLong ()

Returns a pseudorandomly chosen long value.

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//-3072526425556733343
//8849382357415340274
//3101712364887609574
//-1625110490229931621
//6473626241199093806

default long nextLong (long bound)

Returns a pseudorandomly chosen long value between zero (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//516
//913
//191
//283
//332
//244
//59
//761
//760
//939

default long nextLong (long origin, long bound)

Returns a pseudorandomly chosen long value between the specified origin (inclusive) and the specified bound (exclusive).

final var random = RandomGenerator.getDefault();

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

// Result
// ↓
//192
//43
//524
//-341
//-644
//-219
//-434
//985
//536
//-395

static RandomGenerator of (String name)

Returns an instance of RandomGenerator that utilizes the name algorithm.

final var random = RandomGenerator.of("L32X64MixRandom");
System.out.println(random.getClass().getName()); // jdk.random.L32X64MixRandom
final var random = RandomGenerator.of("Random");
System.out.println(random.getClass().getName()); // java.util.Random
final var random = RandomGenerator.of("Xoroshiro128PlusPlus");
System.out.println(random.getClass().getName()); // jdk.random.Xoroshiro128PlusPlus

Related posts

To top of page