Created
May 4, 2013 12:38
-
-
Save jimmy087/5517376 to your computer and use it in GitHub Desktop.
Monte Carlo Simulation a basic program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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