Java : RandomGenerator (乱数) - API使用例
RandomGenerator (Java SE 17 & JDK 17) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。
概要
RandomGeneratorインタフェースは、一連の疑似乱数を生成します。
final var random = RandomGenerator.getDefault();
final var stream = random.ints(5);
stream.forEach(System.out::println);
// 結果
// ↓
//-631306984
//360282856
//1268279487
//345831916
//-296659039
似たAPIとしては Randomクラスがあります。
ただし、Randomクラスは古い乱数生成のアルゴリズムを使っています。
API仕様では、新しいアルゴリズムを使うことがおすすめされています。
また、より強力な乱数が必要となる場合…例えば、暗号化で使う初期化ベクトル(IV)の生成などは、
を代わりに使うことも検討しましょう。
メソッド
default DoubleStream doubles ()
final var random = RandomGenerator.getDefault();
final var stream = random.doubles();
stream.limit(5).forEach(System.out::println);
// 結果
// ↓
//0.16656199123222215
//0.6218217049912959
//0.19600772989497395
//0.555628646104419
//0.7930447771788491
default DoubleStream doubles (double randomNumberOrigin, double randomNumberBound)
final var random = RandomGenerator.getDefault();
final var stream = random.doubles(-3.0, 3.0);
stream.limit(10).forEach(System.out::println);
// 結果
// ↓
//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)
final var random = RandomGenerator.getDefault();
final var stream = random.doubles(5);
stream.forEach(System.out::println);
// 結果
// ↓
//0.8334380086898268
//0.03037022005663459
//0.10880306626740466
//0.32559504023785624
//0.5128375942419151
default DoubleStream doubles (long streamSize, double randomNumberOrigin, double randomNumberBound)
final var random = RandomGenerator.getDefault();
final var stream = random.doubles(10, -3.0, 3.0);
stream.forEach(System.out::println);
// 結果
// ↓
//-2.0006280523452276
//2.496709103408133
//2.7277152710430457
//2.063650144667279
//0.8035359256505972
//-1.5947828941532585
//-0.4565496604566852
//-0.8288139724521377
//2.3173434035873637
//-1.6395531086397552
static RandomGenerator getDefault ()
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);
// 結果
// ↓
//715378305
//101370956
//694682166
//-1355452562
//1273544944
default IntStream ints ()
final var random = RandomGenerator.getDefault();
final var stream = random.ints();
stream.limit(5).forEach(System.out::println);
// 結果
// ↓
//715378305
//-825023944
//-473389675
//234171438
//-1489011721
default IntStream ints (int randomNumberOrigin, int randomNumberBound)
final var random = RandomGenerator.getDefault();
final var stream = random.ints(-1000, 1000);
stream.limit(10).forEach(System.out::println);
// 結果
// ↓
//152
//207
//-685
//-549
//700
//593
//-814
//2
//646
//-774
default IntStream ints (long streamSize)
final var random = RandomGenerator.getDefault();
final var stream = random.ints(5);
stream.forEach(System.out::println);
// 結果
// ↓
//715378305
//-1250363547
//1871257264
//784792419
//1413829192
default IntStream ints (long streamSize, int randomNumberOrigin, int randomNumberBound)
final var random = RandomGenerator.getDefault();
final var stream = random.ints(10, -1000, 1000);
stream.forEach(System.out::println);
// 結果
// ↓
//152
//-644
//497
//-914
//961
//887
//930
//-423
//775
//476
default boolean isDeprecated ()
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 ()
final var random = RandomGenerator.getDefault();
final var stream = random.longs();
stream.limit(5).forEach(System.out::println);
// 結果
// ↓
//3072526424993141147
//-1228477046872454351
//-6421471344833399471
//6602871497182012087
//2316779264487820838
default LongStream longs (long streamSize)
final var random = RandomGenerator.getDefault();
final var stream = random.longs(5);
stream.forEach(System.out::println);
// 結果
// ↓
//3072526424836303138
//-1694446413389799187
//-1584792343893839579
//5466113612786326664
//-769135330838364015
default LongStream longs (long randomNumberOrigin, long randomNumberBound)
final var random = RandomGenerator.getDefault();
final var stream = random.longs(-1000, 1000);
stream.limit(10).forEach(System.out::println);
// 結果
// ↓
//-300
//977
//-611
//-503
//-437
//-99
//713
//-955
//856
//73
default LongStream longs (long streamSize, long randomNumberOrigin, long randomNumberBound)
final var random = RandomGenerator.getDefault();
final var stream = random.longs(10, -1000, 1000);
stream.forEach(System.out::println);
// 結果
// ↓
//-150
//-913
//376
//-870
//-399
//249
//-76
//-299
//-890
//-404
default boolean nextBoolean ()
final var random = RandomGenerator.getDefault();
IntStream.range(0, 5).forEach(i -> {
final var value = random.nextBoolean();
System.out.println(value);
});
// 結果
// ↓
//false
//true
//true
//false
//true
default void nextBytes (byte[] bytes)
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 ()
final var random = RandomGenerator.getDefault();
IntStream.range(0, 5).forEach(i -> {
final var value = random.nextDouble();
System.out.println(value);
});
// 結果
// ↓
//0.16656199121570903
//0.23367465009528576
//0.9284442490070819
//0.36413039663241964
//0.09374887895945716
default double nextDouble (double bound)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextDouble(3.0);
System.out.println(value);
});
// 結果
// ↓
//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)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextDouble(-3.0, 3.0);
System.out.println(value);
});
// 結果
// ↓
//2.0006280525762694
//1.0798768391242595
//-0.5062689333449741
//-0.6804432211100737
//1.319150255507057
//0.3724747688240164
//2.11873513987363
//1.745673342239547
//2.412951541889652
//-2.6840469600622776
default double nextExponential ()
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextExponential();
System.out.println(value);
});
// 結果
// ↓
//1.4853544341415674
//0.2791056072286121
//1.0625803161169822
//1.837581139676294
//1.584304548356971
//1.2168826889961073
//2.350141721243219
//2.0809004851879367
//2.518312711772013
//0.17643183637445614
default float nextFloat ()
final var random = RandomGenerator.getDefault();
IntStream.range(0, 5).forEach(i -> {
final var value = random.nextFloat();
System.out.println(value);
});
// 結果
// ↓
//0.16656196
//0.60083836
//0.21343786
//0.7307061
//0.7694186
default float nextFloat (float bound)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextFloat(3.0f);
System.out.println(value);
});
// 結果
// ↓
//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)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextFloat(-3.0f, 3.0f);
System.out.println(value);
});
// 結果
// ↓
//-2.0006282
//-2.3810701
//0.201478
//-0.76499224
//-1.1443824
//1.2276878
//-1.6110066
//-1.8389
//-1.0752397
//-1.252554
default double nextGaussian ()
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextGaussian();
System.out.println(value);
});
// 結果
// ↓
//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)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextGaussian(1.0, 2.0);
System.out.println(value);
});
// 結果
// ↓
//1.8436990506325182
//-0.2532608050079741
//2.03751904185916
//2.0035285211930285
//3.7245125081288464
//1.565431386311888
//1.4723064254841673
//2.396063711627094
//-1.9759561643517785
//3.3377298846768513
default int nextInt ()
final var random = RandomGenerator.getDefault();
IntStream.range(0, 5).forEach(i -> {
final var value = random.nextInt();
System.out.println(value);
});
// 結果
// ↓
//715378305
//-1167476346
//-928563181
//257107968
//-870146779
default int nextInt (int bound)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextInt(1000);
System.out.println(value);
});
// 結果
// ↓
//152
//419
//93
//875
//521
//662
//829
//476
//727
//985
default int nextInt (int origin, int bound)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextInt(-1000, 1000);
System.out.println(value);
});
// 結果
// ↓
//152
//385
//2
//796
//-239
//-84
//223
//-690
//-408
//-194
long nextLong ()
final var random = RandomGenerator.getDefault();
IntStream.range(0, 5).forEach(i -> {
final var value = random.nextLong();
System.out.println(value);
});
// 結果
// ↓
//-3072526425556733343
//8849382357415340274
//3101712364887609574
//-1625110490229931621
//6473626241199093806
default long nextLong (long bound)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextLong(1000);
System.out.println(value);
});
// 結果
// ↓
//516
//913
//191
//283
//332
//244
//59
//761
//760
//939
default long nextLong (long origin, long bound)
final var random = RandomGenerator.getDefault();
IntStream.range(0, 10).forEach(i -> {
final var value = random.nextLong(-1000, 1000);
System.out.println(value);
});
// 結果
// ↓
//192
//43
//524
//-341
//-644
//-219
//-434
//985
//536
//-395
static RandomGenerator of (String name)
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