通过投掷飞镖来估算圆周率

Estimating pi by throwing darts

基本上我想通过在单位圆上扔飞镖来找到 pi 的估计值。所以我想在单位圆的正 x 和 y 象限上扔飞镖,每次扔飞镖都会给我一个 x 和 y 方向上小于 1 的随机位置。然后我需要找到与原点的距离从那时起。当飞镖到原点的距离小于 1 时计算出 pi 的近似值,这将被视为命中,其他任何情况都将被视为未命中。使用这个数字 pi 是由 (#hits/#of tosses)*4 找到的,因为我只会看圆的 1/4。然后我想打印一个 table,根据飞镖的数量,结果会有所不同被抛出。我的代码在下面,我对很多事情感到困惑,主要是如何收集所有数据并将其全部打印出来,我正在考虑使用 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
}

}

公式#hits/#tosses的想法是正确的,但它必须太小,因为它不可能大于1。事实证明该公式将近似于[=13=的值], 所以 PI 的近似值是 #hits/#tosses*4.

对于每次试验,如果你想在最后得到一个合理的 PI 近似值,“收集所有数据并全部打印出来”是不切实际的,因为这将需要一百万左右尝试完全接近。我发现 1M 的试验给出了一个很好的结果,而 10M 的结果通常可以精确到小数点后 4 位。即使打印几百个单独的试验也没有用,更不用说 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;
    }
}

结果:

Trial: 1  Throws: 10  Hits: 8  Approximation of PI (hits/throws*4): 3.2000
Trial: 2  Throws: 100  Hits: 79  Approximation of PI (hits/throws*4): 3.1600
Trial: 3  Throws: 1000  Hits: 773  Approximation of PI (hits/throws*4): 3.0920
Trial: 4  Throws: 10000  Hits: 7800  Approximation of PI (hits/throws*4): 3.1200
Trial: 5  Throws: 100000  Hits: 78409  Approximation of PI (hits/throws*4): 3.1364
Trial: 6  Throws: 1000000  Hits: 785250  Approximation of PI (hits/throws*4): 3.1410
Trial: 7  Throws: 10000000  Hits: 7852455  Approximation of PI (hits/throws*4): 3.1410

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