JSFiddle - React, Tailwind, and code Playground
JavaScript
const values = []
const tests = 1000
const standardDeviation = (values) => {
const avg = values.reduce((a, b) => { return a + b })/values.length
const squareDiffs = values.map((value) => {
const diff = value - avg;
const sqrDiff = diff * diff
return sqrDiff
});
const avgSquareDiff = squareDiffs.reduce((a, b) => { return a + b })/squareDiffs.length
const stdDev = Math.sqrt(avgSquareDiff);
return stdDev
}
for (var i = tests - 1; i >= 0; i--) {
const g_noise = Math.sqrt(-2 * Math.log(Math.random()))*Math.cos((2*Math.PI) * Math.random())
values.push(g_noise)
if(i === 0) {
const stDev = standardDeviation(values)
const mean = values.reduce((a, b) => { return a + b })/tests
const max = Math.max(...values)
const min = Math.min(...values)
document.body.innerHTML = `<p>stan dev: ${stDev}<br> mean: ${mean}<br> max: ${max}<br> min: ${min}</p>`
}
}