Random Number Generation

by egon

HTML

<div id="center">
    <canvas id="canvas"></canvas>
    <hr />
    <div id="logDiv"></div>
    <script type="text/javascript">
        canvas = document.getElementById("canvas");
        W=400; H=400;
        canvas.width = W;
        canvas.height = H,
        canvas2D = canvas.getContext("2d");
        
        log = (function(){
            var logDiv = document.getElementById("logDiv");
            return function(){
                var args = [];
                for(var i=0; i < arguments.length; i++)
                    args.push( arguments[i] );
                logDiv.innerHTML = "<p>" + JSON.stringify(args) + "</p>" + logDiv.innerHTML;
            };
        })();
        
    </script>
</div>

CSS

body {
    margin : 0 0;
    width  : 100%;
    background: #000;
}
#center {
    align : center;
    margin : 0 auto;
    margin-top : 30px;
    width : 400px;
}

hr {
    color: #fff;
}

#logDiv {
    font:normal 12px/16px Courier New, monospace;
    color: #fff;
    height: 200px;
    overflow: scroll;
}

JavaScript

var curFrame = 0;

function newRandom(A,B,seed){
    return function(){
        seed = (A*seed+B)%(1<<31); return (seed >> 16) & 0x7fff
    }
}

r1 = newRandom(69069, 1, 0)
r2 = newRandom(1103515245, 12345, 0)
r3 = newRandom(214013, 2531011, 0)

Sys = {
    tick : function(){
        canvas2D.fillStyle = "rgba(0,0,0,0.01)";
        canvas2D.fillRect(0,0,W,H);
        
        var x = curFrame % W;        
        var r = r1()/0x7fff;
        canvas2D.fillStyle = "#0e0";
        canvas2D.fillRect(x,r*H|0,2,2);
        r = r2()/0x7fff;
        canvas2D.fillStyle = "#e00";
        canvas2D.fillRect(x,r*H|0,2,2);
        r = r3()/0x7fff;
        canvas2D.fillStyle = "#00e";
        canvas2D.fillRect(x,r*H|0,2,2);
        
        curFrame++;
    }
};

sys = Object.create(Sys);
function tick(){ sys.tick() };
setInterval( tick, 33 );