Java 用投掷飞镖估算pi

Java 用投掷飞镖估算pi,java,pi,Java,Pi,基本上,我想通过在单位圆上投一个飞镖来估算π。所以我想在单位圆的正x和y象限投一个省道,每次投省道都会在x和y方向上给我一个小于1的随机位置。然后我需要找到从原点到该点的距离。圆周率的近似值来自计算时省道与原点的距离小于1时,这将被视为命中,其他任何东西都将被视为未命中。使用这个数字π可以通过(#命中次数/#投掷次数)*4找到,因为我将只看圆的1/4。然后我想打印一个表格,根据投掷的省道数量,打印不同的结果。下面是我的代码,我对很多事情感到困惑,主要是我如何收集所有数据并将其全部打印出来,我正在

基本上,我想通过在单位圆上投一个飞镖来估算π。所以我想在单位圆的正x和y象限投一个省道,每次投省道都会在x和y方向上给我一个小于1的随机位置。然后我需要找到从原点到该点的距离。圆周率的近似值来自计算时省道与原点的距离小于1时,这将被视为命中,其他任何东西都将被视为未命中。使用这个数字π可以通过(#命中次数/#投掷次数)*4找到,因为我将只看圆的1/4。然后我想打印一个表格,根据投掷的省道数量,打印不同的结果。下面是我的代码,我对很多事情感到困惑,主要是我如何收集所有数据并将其全部打印出来,我正在考虑使用int数组的ArrayList,因为它不局限于我想做的试验数量,但我不确定如何进行。任何帮助都是有用的,谢谢

public static void main(String[]args){
    
    int[] darts_Thrown = {10, 100, 1000, 10000, 100000,1000000};
    

   

  //  for( int i = 0; i < points.length; i++){
   //     System.out.print(points[i]);
  //  }



   for (int i = 0; i < darts_Thrown.length;i++){
        
       for (int j = 0; j < darts_Thrown[i]; j++){
        int test = 0;
        double [] points = getPoint();
       // distanceToOrigin(points[0],points[1]);
       // getHits();
       test++;
       System.out.printf("%d",test);
       }
       System.out.printf("%n%s", "hi ");
   }
   
}
public static double[] getPoint()//code to generate x and y and return them in a double array
{
    
    double[] point_X_Y = {Math.random(),Math.random()};

    return point_X_Y;
    
    
}
public static double distanceToOrigin(double x,double y) //code to compute distance from point (x,y) to origin (0,0)
{
   
    
    double distance = Math.sqrt((Math.pow(x,2))+(Math.pow(y,2))); // used to find distance from origin
   
    return distance;
    
    
}
public static int getHits(int n)
{
    int hits = 0;
    int misses=0;
    
//double distance = distanceToOrigin(points[0], points[1]);
    //if (distance < 0){
  //      hits++;
   //     return hits;
  //  }
  //  else{
   //     misses++;
    //return misses;
  //  }
   // return 0;
    //code to get hits given n= number of darts
}
publicstaticvoidmain(字符串[]args){
int[]掷镖={1010010001000010000000000};
//对于(int i=0;i
}

公式
#hits/#pows
在思想上是正确的,但它必须太小,因为它不可能大于1。结果表明,该公式将近似于PI/4的值,因此PI的近似值为
#hits/#twosts*4

对于每一次试验,如果你想在最后得到一个合理的PI近似值,那么“收集所有数据并打印所有数据”是不实际的,因为需要一百万次左右的试验才能接近。我发现1M试验给出了一个相当好的结果,而10M试验通常会给出正确的结果,精确到小数点后4位。即使打印出100个单独的试验也没有用,更不用说100万个了。所以我认为你能真正打印出来的是试验次数,投掷次数,命中次数,以及PI的最终近似值。下面是实现此目的的代码:

public class Test {

    public static void main(String[]args){

        int[] darts_Thrown = {10, 100, 1000, 10000, 100000, 1000000, 10000000};

        for (int i = 0; i < darts_Thrown.length;i++){

            int hits = 0;
            for (int j = 0; j < darts_Thrown[i]; j++){
                double [] points = getPoint();
                double distance = distanceToOrigin(points[0],points[1]);
                if (distance <= 1.0)
                    hits++;
            }
            double pi_est = (double)hits / darts_Thrown[i] * 4.0;

            System.out.printf("Trial: %d  Throws: %d  Hits: %d  Approximation of PI (hits/throws*4): %.4f\n",
                    i, darts_Thrown[i], hits, pi_est);
        }

    }
    public static double[] getPoint()//code to generate x and y and return them in a double array
    {
        final double[] doubles = {Math.random(), Math.random()};
        return doubles;
    }
    public static double distanceToOrigin(double x,double y) //code to compute distance from point (x,y) to origin (0,0)
    {
        double distance = Math.sqrt((Math.pow(x,2))+(Math.pow(y,2))); // used to find distance from origin
        return distance;
    }
}
这个公式很容易推导。对于半径为1以原点为中心的圆,其1/4将位于象限x=0-1,y=0-1。圆内的点距离原点1个单位或更近,因此这些都是“命中”。半径为1的圆的1/4的面积为PI*r^2/4=PI*1^2/4=PI/4。整个目标区域是x*y=1*1=1。所以命中/抛出=(PI/4)/(1)=PI/4。将两边乘以4得到
PI=hits/throws*4
公式
#hits/throws
在思想上是正确的,但它必须太小,因为它不可能大于1。结果表明,该公式将近似于PI/4的值,因此PI的近似值为
#hits/#twosts*4

对于每一次试验,如果你想在最后得到一个合理的PI近似值,那么“收集所有数据并打印所有数据”是不实际的,因为需要一百万次左右的试验才能接近。我发现1M试验给出了一个相当好的结果,而10M试验通常会给出正确的结果,精确到小数点后4位。即使打印出100个单独的试验也没有用,更不用说100万个了。所以我认为你能真正打印出来的是试验次数,投掷次数,命中次数,以及PI的最终近似值。下面是实现此目的的代码:

public class Test {

    public static void main(String[]args){

        int[] darts_Thrown = {10, 100, 1000, 10000, 100000, 1000000, 10000000};

        for (int i = 0; i < darts_Thrown.length;i++){

            int hits = 0;
            for (int j = 0; j < darts_Thrown[i]; j++){
                double [] points = getPoint();
                double distance = distanceToOrigin(points[0],points[1]);
                if (distance <= 1.0)
                    hits++;
            }
            double pi_est = (double)hits / darts_Thrown[i] * 4.0;

            System.out.printf("Trial: %d  Throws: %d  Hits: %d  Approximation of PI (hits/throws*4): %.4f\n",
                    i, darts_Thrown[i], hits, pi_est);
        }

    }
    public static double[] getPoint()//code to generate x and y and return them in a double array
    {
        final double[] doubles = {Math.random(), Math.random()};
        return doubles;
    }
    public static double distanceToOrigin(double x,double y) //code to compute distance from point (x,y) to origin (0,0)
    {
        double distance = Math.sqrt((Math.pow(x,2))+(Math.pow(y,2))); // used to find distance from origin
        return distance;
    }
}

这个公式很容易推导。对于半径为1以原点为中心的圆,其1/4将位于象限x=0-1,y=0-1。圆内的点距离原点1个单位或更近,因此这些都是“命中”。半径为1的圆的1/4的面积为PI*r^2/4=PI*1^2/4=PI/4。整个目标区域是x*y=1*1=1。所以命中/抛出=(PI/4)/(1)=PI/4。将两边乘以4,得到
PI=hits/throws*4

,其中一个原因是你的距离公式是错误的。另一方面,您的原点(以生成随机点的方式)是.5、.5开始小,而且更详细:有一个方法
RunResult throwDarts(int darts){…}
,该方法为这么多省道运行您的算法,并在其自己的列表/数组中跟踪运行期间要跟踪的每个对象,然后返回一个对象,您可以为该对象分配您关心的所有值,例如
类RunResult{int dartsThrown,hits,misses;public RunResult(…){…}
。然后在main函数中,您可以在
ArrayList
或类似文件中捕获这些结果。让方法做一件事:
throwDart
根据需要多次调用
boolean throwDart(){…}
来抛出省道,等等“pi是通过(#点击次数/#投掷次数)找到的”。如果这是真的,那么你必须击中正方形的右边3次多一点