Random Functions
Playing with PRNG
by soulwire
HTML
<canvas id="canvas"></canvas>
JavaScript
var canvas = document.getElementById( 'canvas' );
var ctx = canvas.getContext( '2d' );
var y = 0.0;
function plot( f ) {
for ( var x = 0; x < canvas.width; x++ ) {
ctx.fillStyle = 'hsl(0, 0%, ' + Math.round( f() * 100 ) + '%)'
ctx.fillRect( x, y, 1, 8 );
}
y += 10;
}
function r1() {
return Math.random();
}
function r2() {
var p = Math.random();
return ((p *= 2) < 1) ? 0.5 * Math.pow(2, 10 * (p - 1)) : 0.5 * (2 - Math.pow(2, -10 * (p - 1)));
}
plot( r1 );
plot( r2 );