How this algorithm works: It first generates random coordinates for a point. It then finds the distance of each point from the middle of the circle. If the distance <= 1, then the point is in the circle, otherwise outside the circle. The ratio between the number of points in the circle and the number of total points is close to pi.
I want to make this algorithm run faster. How do I do it?
function calc(iterations) {
pointCircle = 0, total = 0, x = 0, y = 0, distance = 0, pi = 0;
for (let i = 0; i < iterations; i++) {
x = Math.random().toFixed(1);
y = Math.random().toFixed(1);
distance = Math.sqrt(x * x + y * y);
if (distance <= 1) {pointCircle++;}total++;
}
pi = 4 * (pointCircle / total); console.log(pi);
}