VBScript and PowerShell programs to generate pseudo random integers using a 32-bit Multiply With Carry generator. The programs output pseudo random integers equal to or greater than zero and less than 2^32. The period of the generators is about 8.9 x 10^18, which is about 2^63.

Both programs accept three optional parameters. The first is the number of pseudo random integers to display. The default is 10. The second parameter is a seed value between zero and 2^32. The default, if none is provided, is a value based on the system timer. The third optional parameter is a carry value, also between zero and 2^32. The default carry is a value based on the system date and time. The program outputs integers greater than or equal to zero and less than 2^32.

Note: The three parameters are integers. The VBScript program will accept commas, but the PowerShell script will not.

Sometimes you want to generate real numbers between 0 and 1 instead of integers. Or you want numbers in some other range. This can be easily accomplished by modifying the output statements to produce numbers in the desired range. For example, both scripts linked below include alternate output statements, commented out, to produce normalized real numbers greater than or equal to 0 and less than 1. This is accomplished by dividing the random integers by the modulus, 2^32. Only 15 digits after the decimal are displayed, because that is all that VBScript can handle.

The Multiply With Carry generators produce pseudo random integers according to the following:

    Xi = [(A * Xi-1) + Ci-1] Mod M
    Ci = Integer[((A * Xi-1) + Ci-1) / M]
Where:
    Xi-1 is the previous integer (seed value)
    Xi is the next seed integer
    Ci-1 is the previous carry integer
    Ci is the next carry
    A = 4,164,903,690
    M = 2^32 = 4,294,967,296 (the modulus)
Note:
    B = (A * M) - 1 = 17,888,125,139,539,722,239
    P = (B - 1) / 2 = 8,944,062,569,769,861,119
    B and P are both prime.
    The period of the generator is P.
    Period =~ 8.944 * 10^18 =~ 2^63.

There are two cases where this generator will have a period of one. One case is when the initial seed and carry are both zero. The other case is when the initial seed is M - 1 and the initial carry is A -1. The programs avoid both of these situations. The carry values produced by the generator are always less than A. However, the initial carry can be equal to or greater than A. In that case, the initial pair of values (seed and carry) will not be repeated.

The calculations involved to generate the pseudo random integers must be exact. There can be no round off errors. Because VBScript can only represent integers exactly to 53 bits, special techniques are required to handle intermediate calculations. The product A * Xi-1, for example, results in a 64 bit value. The program handles this by breaking the integers into 16-bit high and low parts.

This Multiply With Carry generator passes all of the tests in the Diehard battery of tests of randomness (by George Marsaglia). The VBScript program is linked here:

MWC32.txt <<-- Click here to view or download the program

The equivalent PowerShell script is linked here:

PsMWC32.txt <<-- Click here to view or download the program