commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Artem Barger (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (RNG-5) Create API usage demo example application.
Date Wed, 10 Aug 2016 20:16:20 GMT

    [ https://issues.apache.org/jira/browse/RNG-5?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15415939#comment-15415939
] 

Artem Barger commented on RNG-5:
--------------------------------

Here is the PI calculation

{code:java}
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.rng;

/**
 * The computation estimates the value by computing the probability that
 * a point \( p = (x, y) \) will lie in the circle of radius \( r = 1 \)
 * inscribed in the square of side \( r = 1 \).
 * The probability could be computed by \[ area_{circle} / area_{square} \],
 * where \( area_{circle} = \pi * r^2 \) and \( area_{square} = 4 r^2 \).
 * Hence, the probability is \( \frac{\pi}{4} \).
 * <p>
 * The Monte Carlo simulation will produce \( N \) points.
 * Defining \( N_c \) as the number of point that satisfy \( x^2 + y^2 <= 1 \),
 * we will have \( \frac{N_c}{N} \approx \frac{\pi}{4} \).
 */
public class ComputePiDemo {

    /**
     * Desired approximation error bound
     */
    private double error;

    /**
     * Random source provider.
     */
    private UniformRandomProvider provider;

    /**
     * Number of generated points
     */
    private int generatedNums;

    /**
     * Actual approximation error received in computation.
     */
    private double actualError;

    /**
     * Create Chi-Square test to evaluate PRNG algorithm
     *
     * @param error  - approximation error
     * @param source - random source {@link RandomSource} to indicate the RNG algorithm
     */
    public ComputePiDemo(double error, RandomSource source) {
        this.error = error;
        this.provider = RandomSource.create(source);
    }

    /**
     * Uses quasi Monte-Carlo method to approximate PI value
     *
     * @return approximated pi value
     */
    public double pi() {
        double newValue = 3;
        double oldValue = 0;
        int internal = 0;
        while ((actualError = Math.abs(oldValue - newValue)) > error) {
            double x = provider.nextDouble();
            double y = provider.nextDouble();
            generatedNums++;
            double v = x * x + y * y;
            if (v <= 1) {
                internal++;
            }
            oldValue = newValue;
            newValue = (4d * internal) / generatedNums;
        }

        return newValue;
    }

    /**
     * Returns the amount of numbers required to generate to converge with desired error value.
     *
     * @return - amount of numbers genereted
     */
    public int generatedNums() {
        return generatedNums;
    }

    /**
     * Returns the absolute error of approximated PI value.
     *
     * @return - the actual approximation error
     */
    public double getError() {
        return actualError;
    }


    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Invalid input parameters, expected input format is: ");
            System.out.println("ComputePiDemo <error> <randomSource>");

            System.out.println("<error> - convergence error rate, stop computation when
" +
                    "absolute error between updated PI value and value from " +
                    "previous cycle is less than error value");

            System.out.println("<randomSource> - random source");

            final StringBuilder allowedSourcesNames = new StringBuilder();
            for (RandomSource source : RandomSource.values()) {
                allowedSourcesNames.append(", ").append(source.name());
            }

            System.out.println("\t\t <randomSource> parameter value could be one of:
"
                    + allowedSourcesNames.toString().replaceFirst(", ", ""));

            System.exit(-1);
        }

        final Double error = Double.valueOf(args[0]);

        final RandomSource randomSource = RandomSource.valueOf(args[1]);

        final ComputePiDemo piApp = new ComputePiDemo(error, randomSource);
        System.out.println("Computed PI value = " + piApp.pi()
                + ", required (" + piApp.generatedNums()
                + ") numbers to get error (" + piApp.getError() + ")");
        System.out.println("JDK PI value = " + Math.PI);
    }

}

{code}

> Create API usage demo example application.
> ------------------------------------------
>
>                 Key: RNG-5
>                 URL: https://issues.apache.org/jira/browse/RNG-5
>             Project: Commons RNG
>          Issue Type: Task
>            Reporter: Artem Barger
>
> Need to create a demo application for user guide to demonstrate how to use the API and
show possible application.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message