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