Random number generator engines test

by anvaka

HTML

<script src="http://dl.dropbox.com/u/5313583/Stuff/js/random.js"></script>

JavaScript

var tests = [
    { name : 'Native Random', func : Math.random},
    { name : 'MRG32k3a', func : MRG32k3a()},
    { name : 'Xorshift03', func : Xorshift03()},
    { name : 'KISS07', func : KISS07()},
    { name : 'LFib', func : LFib()},
    { name : 'LFIB4', func : LFIB4()},
    { name : 'Alea', func : Alea()}
];

var sampleSize = 1000000; // 1 000 000

/**
 * Test quality of pseudo random number generator. For explanation 
 * see: http://www.billthelizard.com/2009/05/how-do-you-test-random-number-generator.html
 */
var testPRNG = function(random, sampleSize) {
    var approxPi = function(inCircle, totalPoints) {
        return inCircle / totalPoints * 4.0;
    },
    
    calcError = function(pi) {
        return (pi - Math.PI)/Math.PI * 100;
    };

    // total number of sample points to take
    var numPoints = sampleSize || 10000,
        inRandCircle = 0,
        xr, yr, zr;
    
    for(var i=0; i < numPoints; ++i)
    {
        xr = random();
        yr = random();
    
        zr = (xr * xr) + (yr * yr);
        if(zr <= 1.0)
            inRandCircle++;
    }

    // calculate the Pi approximations
    var randomPi = approxPi(inRandCircle, numPoints),
        // calculate the % error
        randomError = calcError(randomPi);
    return { 
        randomPi: randomPi,
        randomError : randomError
    };
}

for (var i = 0; i < tests.length; ++i){
    var test = tests[i],
        testResult = testPRNG(test.func, sampleSize);
    testResult.name = test.name;
    
    document.write(JSON.stringify(testResult) + '<br />');
}