首页 > 其他 > 详细

使用蒙特卡洛模拟法并行化模拟掷骰子事件

时间:2020-06-15 00:23:47      阅读:101      评论:0      收藏:0      [点我收藏+]

近日在书中看到这个方法,受益良多,分享下
`
public class ManualDiceRolls {
//使用蒙特卡洛模拟法并行化模拟掷骰子事件
private static final int N = 100000000;
private final double fraction;
private final Map<Integer, Double> results;
private final int numberOfThreads;
private final ExecutorService executor;
private final int workPerThread;

public ManualDiceRolls(double fraction, Map<Integer, Double> results, int numberOfThreads, ExecutorService executor, int workPerThread) {
    this.fraction = fraction;
    this.results = results;
    this.numberOfThreads = numberOfThreads;
    this.executor = executor;
    this.workPerThread = workPerThread;
}

public static void main(String[] args) {
    ManualDiceRolls roles = new ManualDiceRolls ();
    roles.simulateDiceRoles ();
}

public ManualDiceRolls() {
    fraction = 1.0 / N;
    results = new ConcurrentHashMap<> ();
    numberOfThreads = Runtime.getRuntime ().availableProcessors ();
    executor = Executors.newFixedThreadPool (numberOfThreads);
    workPerThread = N / numberOfThreads;
}

public void simulateDiceRoles() {
    List<Future<?>> futures = submitJobs ();
    awaitCompletion (futures);
    printResults ();
}

private void printResults() {
    results.entrySet ()
            .forEach (System.out::println);
}

private List<Future<?>> submitJobs() {
    List<Future<?>> futures = new ArrayList<> ();
    for (int i = 0; i < numberOfThreads; i++) {
        futures.add (executor.submit (makeJob ()));
    }
    return futures;
}

private Runnable makeJob() {
    return () -> {
        ThreadLocalRandom random = ThreadLocalRandom.current ();
        for (int i = 0; i < workPerThread; i++) {
            int entry = twoDiceThrows (random);
            accumulateResult (entry);
        }
    };
}

private void accumulateResult(int entry) {
    results.compute (entry, (key, previous) ->
            previous == null ? fraction
                    : previous + fraction
    );
}

private int twoDiceThrows(ThreadLocalRandom random) {
    int firstThrow = random.nextInt (1, 7);
    int secondThrow = random.nextInt (1, 7);
    return firstThrow + secondThrow;
}

private void awaitCompletion(List<Future<?>> futures) {
    futures.forEach ((future) -> {
        try {
            future.get ();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace ();
        }
    });
    executor.shutdown ();
}

}
`

使用蒙特卡洛模拟法并行化模拟掷骰子事件

原文:https://www.cnblogs.com/onexixi/p/13128379.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!