Normally distributed random
An approximation of normal distribution based on the central limit theorem.
by Yair Even Or
HTML
<div id="container"></div>
CSS
html, body{ padding:0; margin:0; }
#container {
position: relative;
width: 400px;
height: 100vh;
border: 1px solid #000;
}
#container div {
position: absolute;
bottom: 0;
width: 2px;
opacity: 0.1;
}
JavaScript
function rndN(n) {
var rand = 0;
for (var i = 0; i < n; i += 1) {
rand += Math.random();
}
// return (rand - n/2) / (n/2);
return rand / n
}
function draw(n) {
var cnt = 600000;
var sample = 200;
var color = '#'+Math.floor(Math.random()*16777215).toString(16);
var numbers = Array(sample).fill(0);
while(cnt--)
numbers[Math.round(sample * rndN(n))]++;
while (sample--) {
$('#container').append($('<div>').css({
left: sample * 2 + 'px',
height: numbers[sample] / 150 + '%',
background: color
}));
}
}
for (var i = 2; i <= 8; i += 1) {
draw(i);
}