计算Pi randomGen [英] Calculating Pi randomGen
本文介绍了计算Pi randomGen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码尝试使用monte carlo方法时出现问题。这就是我到目前为止所看到的,但我认为我的计算中可能存在错误:
我一直在为我计算的PI得到同样的东西而且我在疯狂。我想我会休息一天,也许有新鲜眼睛的人可能知道我哪里出错了。
I am having problems with my code trying to use the monte carlo method. This is what I have so far but think there might be a error in my calculations:
I keep getting the same thing for my calculated PI and I am driving myself mad. I think I am going to take a break for a day and maybe someone with fresh eyes might know where I went wrong.
展开 | 选择 | 换行 | 行号
推荐答案
你只需要''射击'一次,然后你开始你的循环,你用
进行数学运算的结果。我期待这样的事情:
You only ''shoot'' once and then you start your loop and you do the math with
the result of that single shot. I expected something like this:
展开 | 选择 | Wrap | 行号
You只有''射击''一次,然后你开始你的循环,你用
进行数学运算的结果。我期待这样的事情:
You only ''shoot'' once and then you start your loop and you do the math with
the result of that single shot. I expected something like this:
展开 | 选择 | Wrap | 行号
这就是我所拥有但它仍然没有解决的问题计算正确??任何人?我在绳子尽头。我认为程序本身运行得很好,但我只是遇到了一些正确运行的问题。
import java.util。*;
public class CalculatePI
{
public static boolean isInside(double xPos,double yPos)
{
布尔结果;
双倍距离= Math.sqrt((xPos * xPos)+(yPos * yPos));
if(距离< 1)
result = true;
返回(距离< 1);
}
public static double computePI(int numThrows)
{
随机randomGen = new随机(System.currentTimeMillis());
double xPos =(randomGen.nextDouble())* 2 - 1.0;
double yPos =(randomGen.nextDouble())* 2 - 1.0;
boolean isInside = isInside(xPos,yPos);
int hits = 0;
double PI = 0;
for(int i = 0; i< = numThrows; i ++)
{
xPos =(randomGen.nextDouble ())* 2 - 1.0;
yPos =(randomGen.nextDouble())* 2 - 1.0;
if(isInside(xPos,yPos))
{
// hits = hits + 1;
// PI = 4 *(hits / numThrows);
点击++;
}
}
返回PI;
}
public static void main(String [] args)
{
扫描仪读卡器= new Scanner(System.in);
System.out.println(此程序使用蒙特卡罗方法计算PI。);
System.out .print(请输入投掷次数:);
int numThrows = reader.nextInt();
double PI = computePI(numThrows);
double差异= PI - Math.PI;
System.out.println(" throws =" + numThrows +",Computed PI =" + PI +",差异=" +差异);
}
}
This is what I have but it is still not working out the calculations correctly?? Anybody? I am at the end of my rope. I think the program itself works out pretty well but I am just having some problems getting to run correctly.
import java.util.*;
public class CalculatePI
{
public static boolean isInside (double xPos, double yPos)
{
boolean result;
double distance = Math.sqrt((xPos * xPos) + (yPos * yPos));
if (distance < 1)
result = true;
return(distance < 1);
}
public static double computePI ( int numThrows )
{
Random randomGen = new Random (System.currentTimeMillis() );
double xPos = (randomGen.nextDouble()) * 2 - 1.0;
double yPos = (randomGen.nextDouble()) * 2 - 1.0;
boolean isInside = isInside(xPos, yPos);
int hits = 0;
double PI = 0;
for (int i = 0; i <= numThrows; i++)
{
xPos = (randomGen.nextDouble()) * 2 - 1.0;
yPos = (randomGen.nextDouble()) * 2 - 1.0;
if (isInside(xPos, yPos))
{
//hits = hits + 1;
//PI = 4 * (hits/numThrows);
hits++;
}
}
return PI;
}
public static void main (String[] args)
{
Scanner reader = new Scanner (System.in);
System.out.println("This program calculates PI using the Monte Carlo method.");
System.out.print("Please enter number of throws: ");
int numThrows = reader.nextInt();
double PI = computePI(numThrows);
double Difference = PI - Math.PI;
System.out.println ("Number of throws = " + numThrows + ", Computed PI = " + PI + ", Difference = " + Difference );
}
}
这篇关于计算Pi randomGen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文