JSFiddle - React, Tailwind, and code Playground
HTML
<input type="number" id="times" value="400000"/>
<button id="go">simulate</button>
<p id="res"></p>
<p id="time"></p>
JavaScript
function monty(times) {
var x, y, i,
within = 0;
for (i = 0; i < times; i = i + 1) {
x = Math.random();
y = Math.random();
if (x * x + y * y < 1) {
within = within + 1;
}
}
return within * 4 / times;
}
function sim() {
var before, time, res, times;
times = document.getElementById("times").value;
before = Date.now();
res = monty(times);
time = Date.now() - before;
document.getElementById("res").innerHTML = "π ≈ " + res;
document.getElementById("time").innerHTML = time + " ms";
}
document.getElementById("go").onclick = sim;