Visualize PRNG

by amaan

HTML

<canvas id='c' width='512' height='512'></canvas>

JavaScript

// https://gist.github.com/blixt/f17b47c62508be59987b
// http://boallen.com/random-numbers.html

function Random(seed) {
    this._seed = seed % 2147483647;
    if (this._seed <= 0) this._seed += 2147483646;
}

Random.prototype.next = function () {
    this._seed = (this._seed * 16807) & 0xffffffff;
    return (this._seed - 1) | 0;
};

var GRID_SIZE = 512;
var BLOCK_SIZE = 2;

var c = document.getElementById('c').getContext('2d');
var rand = new Random(Math.floor(Math.random() * 2147483647));
var count = 0;

for (var x = 0; x < GRID_SIZE; x += BLOCK_SIZE) {
    for (var y = 0; y < GRID_SIZE; y += BLOCK_SIZE) {
        if (rand.next() < 0) {
            c.fillRect(x, y, BLOCK_SIZE, BLOCK_SIZE);
            count++;
        }
    }
}