Created
August 10, 2015 20:55
-
-
Save MattSurabian/3f41d5b5344c72cdbbc3 to your computer and use it in GitHub Desktop.
Monte Carlo Experiment
This file contains 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
var startTime = Date.now(); | |
var trials = 10000000; | |
var numQuarters = 8; | |
var quarterDelta = 6.667; | |
var quarterRadius = 1.213; | |
var dimeRadius = 0.895; | |
var hit = 0; | |
var quarterLocations = []; | |
for(var j = 0; j < numQuarters; j++) { | |
quarterLocations.push(j * quarterDelta); | |
} | |
for(var i = 0; i < trials; i++) { | |
var startPoint = Math.random() * 60; | |
var dimeL = startPoint - dimeRadius; | |
var dimeR = startPoint + dimeRadius; | |
for(var k = 0; k < numQuarters; k++) { | |
var quarterL = quarterLocations[k] - quarterRadius; | |
var quarterR = quarterLocations[k] + quarterRadius; | |
if (dimeR >= quarterL && dimeR <= quarterR) { | |
hit+=1; | |
continue; | |
} | |
if (dimeL >= quarterL && dimeL <= quarterR) { | |
hit+=1; | |
continue; | |
} | |
} | |
} | |
var runTime = Date.now() -startTime; | |
var misses = trials - hit; | |
console.log("There were " + hit + " hits and " + misses + " misses."); | |
console.log("Ran in " + runTime + "ms"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment