Skip to content

Instantly share code, notes, and snippets.

@peterkhayes
Last active August 29, 2015 13:56
Show Gist options
  • Save peterkhayes/9181416 to your computer and use it in GitHub Desktop.
Save peterkhayes/9181416 to your computer and use it in GitHub Desktop.
monte carlo solution
var integrate = function() {
// return 42;
var iters = 2000;
var d = getGraphDimensions();
var width = d.x.max - d.x.min;
var height = d.y.max - d.y.min;
var xmin = d.x.min;
var ymin = d.y.min;
var hits = 0;
for (var i = 0; i < iters; i++) {
var x = Math.random()*width + xmin;
var y = Math.random()*height + ymin;
if (evalPoint(x, y)) hits++;
}
var boxArea = width*height;
return boxArea*hits/iters;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment