-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Welcome to the SIGSoftDistributedComputing wiki! package com.uiuc.sigsoft.nettest;
/**
-
Created by Pri on 10/11/2014.
-
Edited by Andrew on 10/14/2014 */ public class MonteCarlo { private long runs; private long hits;
public MonteCarlo() { runs = 0; hits = 0; }
public void run(int times) { for (int i = 0; i < times; i++) { double random_x = Math.random(); double random_y = Math.random(); if (random_x * random_x + random_y * random_y <= 1) { hits++; } } }
public long getHits() { return hits; }
public long getRuns() { return runs; }
public double getPortionHits() { return (double)hits/runs; } }
package com.uiuc.sigsoft.nettest;
public class MonteCarloRunner { private static final int STEPS = 1000; public static void main(String[] args) { Runtime runtime = Runtime.getRuntime(); ThreadGroup threads = new ThreadGroup("Monte Carlo"); for (int i = 0; i < runtime.availableProcessors(); i++) { Thread thread = new Thread(threads, new MonteCarloThread(STEPS)); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); }
}
}
package com.uiuc.sigsoft.nettest;
public class MonteCarloThread implements Runnable { private static int count = 0; private final int STEPS_IN_BLOCK; private double results;
public MonteCarloThread(int steps) {
STEPS_IN_BLOCK = steps;
}
public double getResults() {
return results;
}
private void setResults(double results) {
this.results = results;
}
@Override
public void run() {
MonteCarlo accumulator = new MonteCarlo();
while (!Thread.interrupted()) {
accumulator.run(STEPS_IN_BLOCK);
count++;
System.out.println(count);
}
setResults(accumulator.getPortionHits());
}
}