-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGeneratorDemo.java
More file actions
117 lines (96 loc) · 4.7 KB
/
Copy pathRandomGeneratorDemo.java
File metadata and controls
117 lines (96 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package java17.random;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
import java.util.stream.IntStream;
/**
* Java 17 Enhanced Pseudo-Random Number Generators
* Demonstrates the new RandomGenerator API with multiple algorithms
*/
public class RandomGeneratorDemo {
public static void main(String[] args) {
System.out.println("=== Java 17 Enhanced Pseudo-Random Number Generators ===\n");
// 1. Default random generator
System.out.println("1. Default Random Generator");
System.out.println("---------------------------");
RandomGenerator rng = RandomGeneratorFactory.getDefault().create();
System.out.println("Algorithm: " + RandomGeneratorFactory.getDefault().name());
for (int i = 0; i < 5; i++) {
System.out.println("Random int (0-100): " + rng.nextInt(100));
}
// 2. Specific algorithms
System.out.println("\n2. Specific Algorithms");
System.out.println("---------------------");
// L32X64MixRandom - Fast, good quality
RandomGenerator lcg = RandomGeneratorFactory.of("L32X64MixRandom").create();
System.out.println("L32X64MixRandom:");
for (int i = 0; i < 3; i++) {
System.out.println(" Random: " + lcg.nextLong());
}
// L64X128MixRandom - Very fast, excellent quality
RandomGenerator lcg128 = RandomGeneratorFactory.of("L64X128MixRandom").create();
System.out.println("\nL64X128MixRandom:");
for (int i = 0; i < 3; i++) {
System.out.println(" Random: " + lcg128.nextLong());
}
// Xoshiro256PlusPlus - Fast, good quality
RandomGenerator xoshiro = RandomGeneratorFactory.of("Xoshiro256PlusPlus").create();
System.out.println("\nXoshiro256PlusPlus:");
for (int i = 0; i < 3; i++) {
System.out.println(" Random: " + xoshiro.nextLong());
}
// 3. List all available algorithms
System.out.println("\n3. Available Algorithms");
System.out.println("---------------------");
RandomGeneratorFactory.all()
.map(f -> f.name() + " (" + f.group() + ")")
.sorted()
.forEach(System.out::println);
// 4. Splittable for parallel streams
System.out.println("\n4. Splittable Random for Parallel Streams");
System.out.println("------------------------------------------");
RandomGenerator splittable = RandomGeneratorFactory.of("SplittableRandom").create();
// Generate random numbers in parallel
IntStream parallelStream = IntStream.range(0, 10)
.parallel()
.map(i -> splittable.nextInt(100));
System.out.println("Parallel random numbers:");
parallelStream.forEach(n -> System.out.println(" " + n));
// 5. Performance comparison
System.out.println("\n5. Performance Comparison");
System.out.println("------------------------");
comparePerformance();
// 6. Different data types
System.out.println("\n6. Different Data Types");
System.out.println("----------------------");
RandomGenerator gen = RandomGeneratorFactory.getDefault().create();
System.out.println("int: " + gen.nextInt());
System.out.println("long: " + gen.nextLong());
System.out.println("double: " + gen.nextDouble());
System.out.println("float: " + gen.nextFloat());
System.out.println("boolean: " + gen.nextBoolean());
byte[] bytes = new byte[5];
gen.nextBytes(bytes);
System.out.println("bytes: " + java.util.Arrays.toString(bytes));
}
private static void comparePerformance() {
int iterations = 10_000_000;
// Legacy Random
long start = System.currentTimeMillis();
java.util.Random legacyRandom = new java.util.Random();
for (int i = 0; i < iterations; i++) {
legacyRandom.nextInt();
}
long legacyTime = System.currentTimeMillis() - start;
// New RandomGenerator
start = System.currentTimeMillis();
RandomGenerator newRandom = RandomGeneratorFactory.getDefault().create();
for (int i = 0; i < iterations; i++) {
newRandom.nextInt();
}
long newTime = System.currentTimeMillis() - start;
System.out.println("Legacy Random: " + legacyTime + " ms");
System.out.println("New RandomGenerator: " + newTime + " ms");
System.out.println("Improvement: " + String.format("%.2f%%",
(double)(legacyTime - newTime) / legacyTime * 100));
}
}