Skip to content

Instantly share code, notes, and snippets.

@TJkrusinski
Created April 4, 2017 22:22
Show Gist options
  • Save TJkrusinski/60330d587ee2cccac2efee2320a06837 to your computer and use it in GitHub Desktop.
Save TJkrusinski/60330d587ee2cccac2efee2320a06837 to your computer and use it in GitHub Desktop.
Average distance of two random points in a square with side length 1
function compute() {
return distance(points());
}
function distance(points) {
var deltaY = points[3] - points[1];
var deltaX = points[2] - points[0];
return Math.sqrt(Math.pow(deltaY, 2) + Math.pow(deltaX, 2));
}
function points() {
return [
Math.random(),
Math.random(),
Math.random(),
Math.random()
]
}
var total = 0;
for (var i = 0; i<10000000; i++) {
total += compute();
}
console.log(total / 10000000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment