「蒙特卡洛java」蒙特卡洛算法
今天给各位分享蒙特卡洛java的知识,其中也会对蒙特卡洛算法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
编写程序,根据JAVA的随机采样方法,利用蒙特卡洛仿真方法求圆周率的近似值,
public class MonteCarloMethod {
private static void computePi() {
int count = 0;
int total = 50000;
for (int i = 1; i = total; i++) {
double x = Math.random();
double y = Math.random();
if (Math.pow(x, 2) + Math.pow(y, 2) = 1) {
count = count + 1;
}
}
double mc_pi = 4 * ((count + 0d) / total);
System.out.println(" jdk π = " + Math.PI);
System.out.println("MonteCarlo π = " + mc_pi);
System.out.println(" error = " + Math.abs(mc_pi - Math.PI) / Math.PI);
}
public static void main(String[] args) {
computePi();
}
}
求蒙特卡洛计算 圆周率的 java程序
原理:
根据圆面积的公式: s=πR2 ,当R=1时,S=π。
由于圆的方程是:x^2+y^2=1(x^2为X的平方的意思),因此1/4圆面积为x轴、y轴和上述方程所包围的部分。
如果在1*1的矩形中均匀地落入随机点,则落入1/4园中的点的概率就是1/4圆的面积。其4倍,就是圆面积。
由于半径为1,该面积的值为π的值。
程序如下:
import java.util.Random;
public class pai {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int N = 100000; /*定义随机点数*/
int n = 0,i,resulttimes;
double r;
double x,y; /*坐标*/
Random s = new Random();
for(resulttimes=0;resulttimes10;resulttimes++){ /*输出十次结果*/
for (i=1;i=N;i++)
{
x=s.nextDouble(); /*在0~1之间产生一个随机x坐标*/
y=s.nextDouble(); /*在0~1之间产生一个随机y坐标*/
if(x*x+y*y=1.0) n++; /*统计落入单位圆中的点数*/
}
r=(double)n/N;
System.out.println("The result of pai is "+r*4); /*计算出π的值*/
n=0;
}
}
}
编写程序:计算π的近似值,π的计算公式为
#includestdio.h
main()
{ int n,i; double t,
sum;/*1*/
printf("请输入n的值\n");
scanf("%d",n);
sum=2; i=1; t=2;/*2*/
while(in) { t=t*(2*i)*(2*i)/(2*i-1)/(2*i+1);
/*3*/ // sum=sum*t; i=i+1; }
printf("π的值=%f\n",t);/*4*/ }
或。
写一个Java程序来实现蒙特卡洛法求π的近似值:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MonteCarloPi {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many darts shoud I toss at the board?\n");
String s = br.readLine();
int numberOfDarts = Integer.parseInt(s.trim());
double radius = 1.0;
Dartboard d = new Dartboard(radius);
for(int i=1; i=numberOfDarts; i++){
Toss t = Toss.getRandom(radius);
d.strike(t);
}
double fractionIn = d.getFractionIn();
double pi = 4.0 * fractionIn;
System.out.println("Pi is approximately "+pi);
}
}
class Dartboard{
private double radius;
private int insideCircle, outsideCircle;
public Dartboard(double radius){
this.radius = radius;
insideCircle = 0;
outsideCircle = 0;
}
public void strike(Toss toss){
double x = toss.getX();
double y = toss.getY();
if(Math.sqrt(x*x + y*y) radius)
insideCircle++;
else
outsideCircle++;
}
public double getFractionIn() {
double total = (double) (insideCircle + outsideCircle);
return (double) insideCircle/total;
}
}
class Toss{
private double x,y;
public Toss(double x, double y){
this.x = x;
this.y = y;
}
public double getX(){return x;}
public double getY(){return y;}
public static Toss getRandom(double radius){
double x,y;
double size = Math.random();
double sign = Math.random();
size = size * radius;
if(sign 0.5)
x = size;
else
x = -size;
size = Math.random();
sign = Math.random();
size = size * radius;
if(sign 0.5)
y = size;
else
y = -size;
return new Toss(x,y);
}
}
扩展资料:
C语言:用循环结构分别编写程序
#include
void main()
{
\x09int n=1;
\x09float temp;
\x09float sum=0;
\x09do
\x09{
\x09\x09temp=(float)1/(2*n-1);
\x09\x09if(n%2==1)
\x09\x09\x09sum+=temp;
\x09\x09else
\x09\x09\x09sum-=temp;
\x09\x09n++;
\x09}while(temp=0.000001);
\x09printf("Pi=%f\n",sum*4);
}
蒙特卡洛java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于蒙特卡洛算法、蒙特卡洛java的信息别忘了在本站进行查找喔。