二是快速。简单和快速,是蒙特卡罗方法在现代项目管理中获得应用的技术基础。
蒙特卡罗分析由于概率计算是相对独立互不影响的,所以很容易发展成大规模并行计算的算法。下面是一个多线程计算圆周率的版本:
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Random;
public class ThreadMontePi implements Runnable {
static ArrayList
private String name;
private int threadnum;
public void setName(int threadnum) {
this.name = threadnum + "";
this.threadnum = threadnum;
}
public void run() {
long randSeed = System.currentTimeMillis() - threadnum * 10; // 按时间取随机数种子
//System.out.println(randSeed); //显示随机数种子
Random rand = new Random(randSeed);
double x, y = 0;
long n = 0, i = 0;
for (i = 0; i < 100000000; i++) {
x = (double) rand.nextInt(10000000); // 产生0-10000000的随机整数,不包含10000000
x = x / 10000000d;
// x= Math.random()*(1-0)+0; //产生0-1的随机数,不包含1
// y=Math.random();
y = (double) rand.nextInt(10000000); // 产生0-10000000的随机整数,不包含10000000
y = y / 10000000d;
if (x * x + y * y < 1)
n++;
// System.out.println(y);
}
System.out.println(name + ":" + i + ":" + n);
Hashtable
tabt.put("i", i);
tabt.put("n", n);
array1.set(threadnum, tabt);
double pi = 4 * (double) n / (double) i;
System.out.println(name + ":" + pi);
// System.out.println("hello " + name);
}
public static void main(String[] args) {
//初始化array1
array1.add(new Hashtable());
array1.add(new Hashtable());
array1.add(new Hashtable());
array1.add(new Hashtable());
//启动3个线程计算
ThreadMontePi myThread1 = new ThreadMontePi();
ThreadMontePi myThread2 = new ThreadMontePi();
ThreadMontePi myThread3 = new ThreadMontePi();
myThread1.setName(1);
Thread thread = new Thread(myThread1);
thread.start();
myThread2.setName(2);
Thread thread2 = new Thread(myThread2);
thread2.start();
myThread3.setName(3);
Thread thread3 = new Thread(myThread3);
thread3.start();
//判断线程结束,收集计算结果
boolean flag = true;
while (flag) {
Thread.State state = thread.getState();
Thread.State state2 = thread2.getState();
Thread.State state3 = thread3.getState();
if (java.lang.Thread.State.TERMINATED == state
&& java.lang.Thread.State.TERMINATED == state2
&& java.lang.Thread.State.TERMINATED == state3)
break;
}
Hashtable
System.out.println("thread1:"+tabt.get("i")+","+tabt.get("n"));
Hashtable
System.out.println("thread2:"+tabt2.get("i")+","+tabt2.get("n"));
Hashtable
System.out.println("thread3:"+tabt3.get("i")+","+tabt3.get("n"));
long sumI=tabt.get("i")+tabt2.get("i")+tabt3.get("i");
long sumN=tabt.get("n")+tabt2.get("n")+tabt3.get("n");
double pi = 4 * (double) sumN / (double) sumI;
System.out.println("sumPi:"+pi); //输出最终计算结果
}
}
可能的输出结果为:
2:100000000:78545069
2:3.14180276
3:100000000:78536096
3:3.14144384
1:100000000:78540560
1:3.1416224
thread1:100000000,78540560
thread2:100000000,78545069
thread3:100000000,78536096
sumPi:3.141623