基本上,我想通过在单位圆上投掷飞镖来找到pi的估计值。因此,我想向单位圆的正x和y象限投掷飞镖,投掷的每个飞镖都会在x和y方向上给我一个小于1的随机位置。然后,我需要找到距原点的距离从那时开始。 pi的近似值来自于在计算时飞镖距原点的距离小于1时,这将算作命中,其他任何因素都将算作未命中。使用这个数字pi是由(#hits /#of tosses)* 4找到的,因为我只看圆圈的1/4。然后,我想打印一张桌子,结果取决于扔了多少支飞镖。我的代码在下面,我对很多事情感到困惑,主要是如何收集所有数据并全部打印出来,我在考虑使用一个由整数组成的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
}
}
答案 0 :(得分:2)
公式#hits/#tosses
在思想上是正确的,但是它太小了,因为它不可能大于1。事实证明,该公式将近似于PI/4
的值,因此PI的近似值为#hits/#tosses*4
。
对于每个试验,“收集所有数据并全部打印”实际上是不现实的,如果您想在最后获得合理的PI近似值,因为要进行一百万次左右的试验完全关闭。我发现1M试用提供了相当不错的结果,而10M试用通常会为您提供正确的小数点后4位的结果。即使打印几百个单独的试用版,也没有用,其中少于一百万。因此,我认为您真正可以打印的只是试验编号,投掷次数,命中次数以及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个单位,因此都是“命中”。半径为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
。