Normally distributed random
An approximation of normal distribution based on the central limit theorem.
HTML
<div id="container"></div>
CSS
#container {
position: relative;
width: 400px;
height: 800px;
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 = [];
for (var i = 0; i < sample; i++) numbers[i] = 0;
for (var i = 0; i < cnt; i++) {
numbers[Math.round(sample * rndN(n))]++;
}
for (var i = 0; i < sample; i++) {
$('#container').append($('<div>').css({
left: i * 2 + 'px',
height: numbers[i] / 25 + 'px',
background: color
}));
}
}
for (var i = 0; i <= 12; i += 1) {
draw(i);
}