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

// https://stackoverflow.com/a/39187274/104380

function rndN(n) {
	var rand = 0, i;
  for (i = n; i--;)	rand += Math.random()
  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);
}