JSFiddle - React, Tailwind, and code Playground

by mvasilkov

HTML

<div id="container"></div>
<p>min=<span id="min"></span> max=<span id="max"></span></p>

CSS

.col {
    background: #0080ff;
    display: inline-block;
    width: 3px;
}

.nope {
    background: #ff0080;
    height: 20px;
    margin: 0 1px;
    width: 1px;
}

#col0 { background: #8000ff }

JavaScript

function $id(id) { return document.getElementById(id) }

var res = {}
var min = +Infinity, max = -Infinity

function inc(x) {
    if (!res[x]) res[x] = 1
    else ++res[x]
}

function compute() {
    var n, i
    for (i = 0; i < 1000000; ++i) {
        inc('' + parseInt((10 * (n = rng.standardNormal())).toFixed(), 10))
        if (n < min) min = n
        if (n > max) max = n
    }
}

function visual() {
    var c = $id('container'), i, col
    for (i = -100; i <= 100; ++i) {
        col = document.createElement('div')
        col.id = 'col' + i
        col.className = 'col'
        if (res['' + i])
            col.style.height = parseInt(res['' + i] / 100, 10) + 'px'
        else
            col.className += ' nope'
        c.appendChild(col)
    }
    $id('min').appendChild(document.createTextNode(min))
    $id('max').appendChild(document.createTextNode(max))
}

var rng = function () {
    'use strict'

    function ParkMiller() {
        this.s = 1
        this.cache = NaN
    }

    ParkMiller.prototype.seed = function seed(a) {
        if (a > 1)
            return (this.s = a % 2147483647), this
        return (this.s = 1), this
    }

    ParkMiller.prototype.iuniform = function iuniform() {
        return (this.s = this.s * 16807 % 2147483647)
    }

    ParkMiller.prototype.uniform = function uniform() {
        return this.iuniform() / 2147483647
    }

    ParkMiller.prototype.standardNormal = function standardNormal() {
        var u, v, fac
        if (!isNaN(this.cache)) {
            fac = this.cache
            this.cache = NaN
            return fac
        }
        do {
            u = this.iuniform() / 1073740000 - 1
            v = this.iuniform() / 1073740000 - 1
            fac = u * u + v * v
        }
        while (fac >= 1 || !fac)
        fac = Math.sqrt(-2 * Math.log(fac) / fac)
        this.cache = fac * u
        return fac * v
    }

    return (new ParkMiller).seed(Date.now())
}()

compute()
visual()