Skip to content

Commit 34369b0

Browse files
committed
Feature: Monte Carlo simulation - use better radius
1 parent 13a1011 commit 34369b0

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

22
{
33
// Monte Carlo simulation
4-
function calculatePI(cycles: number): number {
4+
function calculatePI(cycles: number, r: number): number {
55
let inside = 0;
66

77
for (let i = 0; i < cycles; i++) {
8-
let x = Math.random() * 2 - 1;
9-
let y = Math.random() * 2 - 1;
8+
let x = Math.random() * r;
9+
let y = Math.random() * r;
1010

11-
if ((x*x + y*y) < 1) {
11+
if ((x*x + y*y) < r*r) {
1212
inside++
1313
}
1414
}
@@ -17,5 +17,5 @@
1717
}
1818

1919
console_log("Monte Carlo simulation");
20-
console_log(calculatePI(10000000));
20+
console_log(calculatePI(1e8, 2 ** 16));
2121
}

0 commit comments

Comments
 (0)