Generating Psuedorandom Numbers Using a Linear Congruential Generator

A linear congruential generator uses a relatively simple and easy-to-understand algorithm to generate seemingly random numbers.  It’s no Mersenne twister, though, so I wouldn’t use it for anything you might conceivably get sued or sent to jail over, like scrambling incriminating hard drive data or simulating the effects of air turbulence on your billion-dollar rocketship.

1. In any convenient location, create a new folder named “RandomizerLCGTest”.

2. In the newly created “RandomizerLCGTest” directory, create a new text file named “RandomizerLCGTest.js”, containing the following text:

main();

function main()
{
    var lcg = new LinearCongruentialGenerator
    (
        1103515245,         // multiplier
        12345,             // addend
        Math.pow(2.0, 31),    // modulus
        0.1            // firstRandom
    );

    var newline = "<br />";

    document.write(lcg.toString() + newline);

    var numberOfRandomsToGenerate = 16;

    document.write
    (
        "The first " 
        + numberOfRandomsToGenerate 
        + " random numbers generated are: " 
        + newline
    );

    for (var i = 0; i < numberOfRandomsToGenerate; i++)
    {
        document.write(lcg.getNextRandom() + newline);
    }
}


function LinearCongruentialGenerator(multiplier, addend, modulus, firstRandom)
{
    this.multiplier = multiplier;
    this.addend = addend;
    this.modulus = modulus;
    this.currentRandom = firstRandom;

    this.getNextRandom = function()
    {
        this.currentRandom = 
        (

            (
                this.multiplier 
                * (this.currentRandom * this.modulus)
                + this.addend
            ) 
            % this.modulus
        ) 
        / this.modulus;


        return this.currentRandom;
    }

    this.toString = function()
    {
        var returnValue = 
            "&lt;LinearCongruentialGenerator "
            + "multiplier='" + this.multiplier + "' "
            + "addend='" + this.addend + "' "
            + "modulus='" + this.modulus + "' "
            + "currentRandom='" + this.currentRandom + "' "
            + "/&gt;";

        return returnValue;
    }
}

3. Still in the RandomizerLCGTest directory, create a new text file named “RandomizerLCGTest.html”, containing the following text:

<html>
    <body>
        <script type='text/javascript' src='RandomizerLCGTest.js'></script>
    </body>
</html>

4. Start a web browser and open the newly created “RandomizerLCGTest.html” file in it.

5. Verify that a list of 16 seemingly random numbers is displayed.

<LinearCongruentialGenerator multiplier='1103515245' addend='12345' modulus='2147483648' currentRandom='0.1' />
The first 16 random numbers generated are: 
0.5000057518482208
0.7522042989730835
0.2713412046432495
0.9204963445663452
0.19574058055877686
0.7117666304111481
0.5409882068634033
0.6389849185943604
0.9939663410186768
0.33098435401916504
0.5166313648223877
0.12666726112365723
0.6923573315143585
0.3136192560195923
0.14318382740020752
0.37358346581459045
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s