Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Random/RandomTestMoments.java
41149 views
1
/*
2
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
import java.util.ArrayList;
24
import java.util.List;
25
26
import java.util.random.*;
27
28
import java.util.Set;
29
import java.util.concurrent.CompletableFuture;
30
import java.util.concurrent.ExecutionException;
31
import java.util.concurrent.ThreadLocalRandom;
32
import java.util.concurrent.TimeUnit;
33
import java.util.concurrent.TimeoutException;
34
import java.util.function.DoubleSupplier;
35
import java.util.stream.Stream;
36
37
import static java.util.stream.Collectors.toList;
38
import static java.util.stream.Collectors.toSet;
39
40
/**
41
* @test
42
* @summary test bit sequences produced by clases that implement interface RandomGenerator
43
* @bug 8248862
44
* @run main RandomTestMoments
45
* @author Guy Steele
46
* @key randomness
47
*/
48
49
public class RandomTestMoments {
50
51
static String currentRNG = "";
52
static int failCount = 0;
53
54
static void exceptionOnFail() {
55
if (failCount != 0) {
56
throw new RuntimeException(failCount + " fails detected");
57
}
58
}
59
60
static void setRNG(String rng) {
61
currentRNG = rng;
62
}
63
64
static void fail(String format, Object... args) {
65
if (currentRNG.length() != 0) {
66
System.err.println(currentRNG);
67
currentRNG = "";
68
}
69
70
System.err.format(" " + format, args);
71
failCount++;
72
}
73
74
static final int SEQUENCE_SIZE = 50000;
75
76
// Moment k of uniform distribution over [0.0,1.0) is 1.0/(1+k).
77
78
static double[][] momentsUniform = {
79
{ 1.00000, 1.00000, 0.01000 },
80
{ 0.500000, 0.500000, 0.0266265 },
81
{ 0.333333, 0.333333, 0.0391128 },
82
{ 0.250000, 0.250000, 0.0477151 },
83
{ 0.200000, 0.200000, 0.0540496 },
84
{ 0.166667, 0.166667, 0.0589355 },
85
{ 0.142857, 0.142857, 0.0628462 },
86
{ 0.125000, 0.125000, 0.0660693 },
87
{ 0.111111, 0.111111, 0.0688036 },
88
{ 0.100000, 0.100000, 0.0712002 },
89
{ 0.0909091, 0.0909091, 0.0733755 },
90
{ 0.0833333, 0.0833333, 0.0754172 },
91
{ 0.0769231, 0.0769231, 0.0773868 },
92
{ 0.0714286, 0.0714286, 0.0793244 },
93
{ 0.0666667, 0.0666667, 0.0812526 },
94
{ 0.0625000, 0.0625000, 0.0831806 },
95
};
96
97
// Moment k of exponential distribution with mean 1 is k!.
98
99
static double[][] momentsExponential = {
100
{ 1.00000, 1.00000, 0.01000 },
101
{ 1.00000, 1.00000, 0.0718997 },
102
{ 2.00000, 2.00000, 0.153241 },
103
{ 6.00000, 6.00000, 0.282813 },
104
{ 24.0000, 24.0000, 0.503707 },
105
};
106
107
108
// Absolute moment k of Gaussian distribution with mean 0 and stddev 1 is
109
// pow(2.0,k/2.0)*gamma((k+1)/2.0)/sqrt(PI)
110
// https://arxiv.org/pdf/1209.4340.pdf, equation (18)
111
112
static double[][] absoluteMomentsGaussian = {
113
{ 1.00000, 1.00000, 0.01 },
114
{ 0.797885, 0.797885, 0.1 },
115
{ 1.00000, 1.00000, 0.1 },
116
{ 1.59577, 1.59577, 0.2 },
117
{ 3.00000, 3.00000, 0.2 },
118
{ 6.38308, 6.38308, 0.2 },
119
{ 15.0000, 15.0000, 0.2 },
120
{ 38.2985, 38.2985, 0.2 },
121
{ 105.000, 105.000, 0.4 },
122
};
123
124
static void checkMoments(String test, double[] measurements, double[][] standard) {
125
for (int j = 0; j < measurements.length; j++) {
126
double actual = measurements[j];
127
double expected = standard[j][0];
128
double basis = standard[j][1];
129
double tolerance = standard[j][2];
130
if (!(Math.abs(actual - expected)/basis < tolerance)) {
131
fail("%s fail: actual=%f, expected=%f, basis=%f, tolerance=%f\n",
132
test, actual, expected, basis, tolerance);
133
}
134
}
135
}
136
137
static void testMomentsGaussian(DoubleSupplier theSupplier) {
138
int m = absoluteMomentsGaussian.length;
139
double[] absoluteMoments = new double[m];
140
for (int j = 0; j < SEQUENCE_SIZE; j++) {
141
double v = theSupplier.getAsDouble();
142
double z = 1.0;
143
for (int k = 0; k < m; k++) {
144
absoluteMoments[k] += Math.abs(z);
145
z *= v;
146
}
147
}
148
for (int k = 0; k < m; k++) {
149
absoluteMoments[k] /= SEQUENCE_SIZE;
150
}
151
checkMoments("Gaussian", absoluteMoments, absoluteMomentsGaussian);
152
}
153
154
static void testMomentsExponential(DoubleSupplier theSupplier) {
155
int m = momentsExponential.length;
156
double[] moments = new double[m];
157
for (int j = 0; j < SEQUENCE_SIZE; j++) {
158
double v = theSupplier.getAsDouble();
159
double z = 1.0;
160
for (int k = 0; k < m; k++) {
161
moments[k] += z;
162
z *= v;
163
}
164
}
165
for (int k = 0; k < m; k++) {
166
moments[k] /= SEQUENCE_SIZE;
167
}
168
checkMoments("Exponential", moments, momentsExponential);
169
}
170
171
static void testMomentsUniform(DoubleSupplier theSupplier) {
172
int m = momentsUniform.length;
173
double[] moments = new double[m];
174
for (int j = 0; j < SEQUENCE_SIZE; j++) {
175
double v = theSupplier.getAsDouble();
176
double z = 1.0;
177
for (int k = 0; k < m; k++) {
178
moments[k] += z;
179
z *= v;
180
}
181
}
182
for (int k = 0; k < m; k++) {
183
moments[k] /= SEQUENCE_SIZE;
184
}
185
checkMoments("Uniform", moments, momentsUniform);
186
}
187
188
static void testOneRng(RandomGenerator rng) {
189
testMomentsGaussian(rng::nextGaussian);
190
testMomentsExponential(rng::nextExponential);
191
testMomentsUniform(rng::nextDouble);
192
testMomentsUniform(rng.doubles().iterator()::next);
193
}
194
195
public static void main(String[] args) {
196
RandomGeneratorFactory.all()
197
.forEach(factory -> {
198
setRNG(factory.name());
199
testOneRng(factory.create(325) );
200
});
201
202
exceptionOnFail();
203
}
204
205
}
206
207