Using Java random numbers to calculate pi π

Source: Internet
Author: User
Tags diff

I. Theory Article 1. Mathematical formulas

Circle Area formula: Π*r*r, where π is pi, R is the circle radius;

Square area formula: S*s, where S is the edge length;

Pythagorean Theorem: a*a + b*b = c*c, where A/b is right triangle two right-angled edges, and C is the hypotenuse.

2. Calculation method

Consider that the square with the side length r is embedded with a 1/4 circle with a radius of R.

650) this.width=650; "title=" image "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M00/59/B4/wKiom1TcTVOiSBt2AAB7dqvy4e8045.jpg" width= "466" height= "435"/>

Insidecircle area = Circle area with radius of R/4 =Π*R*R/4

Square area = R*r

So, insidecircle area/Square area = (Π*R*R/4)/(R*R) =Π/4

The polygons consist of lines, and the lines consist of dots. Therefore, if there are several points evenly falling into the square, then the number of points falling into the insidecircle will be Π/4, thus the value of π will be counted.

Second, the actual combat Chapter 1. Shotgun calculation

The concrete way is: make a like the same square plank, with the shotgun against it a random sweep, and finally count the number of bullet holes and landing point, so as to obtain the value of π.

In fact, there are really researchers who have done experiments with the brain hole larger than black holes, and they got 3.13 in 30,857 samples, which is a good result.

See: Http://www.techug.com/compute-pi-with-gun

2. Java Random number calculation

As a yard farmer, of course, can not afford to play shotgun this high-grade toys, then the next code to play a piece.

The specific idea is this: R directly takes a value of 1.0, also need to define a drop in the square of all points of the number pointnumber, each point has a coordinate (x, y), x. Y takes a value of 0.0-1.0, using the Java random number to generate each point, and then use the Pythagorean theorem to determine whether the point falls within the circle or the circle, and the number of points falling within the circle Insidecirclenumber, then π=insidecirclenumber/ Pointnumber*4. Of course, if the calculation is only one time, the error may be larger, you can increase the number of calculations Calctimes, and then the average.

The following results are calculated as follows:

Pointnumber

Calctimes

Pi

The closest value to π

10000

10000

3.141617279999959

3.1416

100000

10000

3.1415569599999684

3.1416

1000000

10000

3.1415845499999953

3.141592

10000000

10000

3.1415924761886806

3.1415928

Attached source code:

Package Com.test.pai;

Import Org.apache.commons.lang.math.RandomUtils;

public class Calcpai
{
public static Boolean incircle (Double x, double y)
{
Return (y <= math.sqrt (1-x * x));
}

public static double calcpaibypointnumber (long num)
{
Double incirclenum = 0.0;
for (long i = 0; i < num; i++)
{
if (Calcpai.incircle (randomutils.nextdouble (), randomutils.nextdouble ()))
{
incirclenum++;
}
}

Double pai = incirclenum * 4/num;

Return pai;
}

public static void Main (string[] args)
{
Double Realpai = 3.14159265;
Currresult Currresult = new Currresult (0.0, Realpai, 0.0);
Long times = 10000;
Long num = 1000000;
for (Long i = 1; I <= times; i++)
{

Double pai = calcpai.calcpaibypointnumber (num);
Currresult.settotalpai (Currresult.gettotalpai () + pai);
Double diff = Math.Abs (realpai-pai);
if (diff < currresult.getdifference ())
{
Currresult.setcurrpai (PAI);
Currresult.setdifference (diff);
}

System.out.println ("No." + i + "/" + times + "\ T" + pai + "\ T" + currresult.getcurrpai () + "\ T"
+ Currresult.gettotalpai ()/i);
}

}

}

This article is from the "fireworks easy to cool" blog, please be sure to keep this source http://yuanhuan.blog.51cto.com/3367116/1614051

Using Java random numbers to calculate pi π

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.