Skip to content

Instantly share code, notes, and snippets.

@jimmy087
Created May 4, 2013 12:38
Show Gist options
  • Save jimmy087/5517376 to your computer and use it in GitHub Desktop.
Save jimmy087/5517376 to your computer and use it in GitHub Desktop.
Monte Carlo Simulation a basic program
import java.util.Scanner;
//Monte Carlo Simulation.
public class MonteCarloSimulation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double numberOfSimulation = 1000000;
int numberOfHits = 0;
for (int i = 0; i < numberOfSimulation; i = i + 1) {
double x = Math.random() * 2 - 1;
double y = Math.random() * 2 - 1;
if(x * x + y * y < 1) numberOfHits = numberOfHits + 1;
}
double pi = 4.0 * numberOfHits / numberOfSimulation;
System.out.print("The value of PI is: " + pi);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment