Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Random/RandomCanaryPi.java
41149 views
1
/*
2
* Copyright (c) 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
24
import java.util.Comparator;
25
import java.util.random.RandomGenerator;
26
import java.util.random.RandomGenerator.*;
27
import java.util.random.RandomGeneratorFactory;
28
29
/**
30
* @test
31
* @summary test bit sequences produced by clases that implement interface RandomGenerator
32
* @bug 8248862
33
* @run main RandomCanaryPi
34
* @key randomness
35
*/
36
37
public class RandomCanaryPi {
38
static double pi(RandomGenerator rng) {
39
int N = 10000000;
40
int k = 0;
41
42
for (int i = 0; i < N; i++) {
43
double x = rng.nextDouble();
44
double y = rng.nextDouble();
45
46
if (x * x + y * y <= 1.0) {
47
k++;
48
}
49
}
50
51
return 4.0 * (double)k / (double)N;
52
}
53
54
static int failed = 0;
55
56
public static void main(String[] args) {
57
RandomGeneratorFactory.all()
58
.sorted(Comparator.comparing(RandomGeneratorFactory::name))
59
.forEach(factory -> {
60
RandomGenerator rng = factory.create();
61
double pi = pi(rng);
62
double delta = Math.abs(Math.PI - pi);
63
boolean pass = delta < 1E-2;
64
65
if (!pass) {
66
System.err.println("Algorithm = " + factory.name() + " failed");
67
System.err.println("Actual = " + Math.PI);
68
System.err.println("Monte Carlo = " + pi);
69
System.err.println("Delta = " + delta);
70
System.err.println();
71
72
failed++;
73
}
74
});
75
if (failed != 0) {
76
throw new RuntimeException(failed + " tests failed");
77
}
78
}
79
}
80
81