/* * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * This source code is provided to illustrate the usage of a given feature * or technique and has been deliberately simplified. Additional steps * required for a production-quality application, such as security checks, * input validation and proper error handling, might not be present in * this sample code. */ package stream.parallel; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.LongStream; /** * This demo shows how to use the parallel mode and the Monte Carlo method to * calculate the value of PI * * @author tyan */ public class MonteCarloPI { public static void main(String[] args) { System.out.println("This might take several minutes, " + "depending on your system"); LongStream.iterate(10l, l -> 10 * l). limit(10). forEach(times -> { System.out.printf( "The value of PI is %1.14f after %,d times calculation\n", pi(times), times); }); } /** * Use the Monte Carlo method to calculate the value of PI. basic algorithm * is: 1. Draw a square on the ground, then inscribe a circle within it. 2. * Scatter some objects of uniform size (grains of rice or sand) over the * square. 3. Count the total number of objects inside the circle and the * total number of objects overall. 4. The ratio of the two total is an * estimate of the ratio of the two areas, which is PI/4. Multiply the * result by 4 to estimate PI. * * @param x how many times randomly selected a point * @return value of π by x times calculation */ private static double pi(long N) { long M = LongStream.range(0, N).parallel().filter(sr -> { // Random picked up point to check if it's in the circle. double x = ThreadLocalRandom.current().nextDouble(-1, 1); double y = ThreadLocalRandom.current().nextDouble(-1, 1); return x * x + y * y < 1.0 * 1.0; // Not using }).count(); return 4.0 * M / N; } }