Created
April 4, 2017 22:22
-
-
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
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
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