VBScript program that duplicates the VBScript Rnd function, the built-in pseudo random number generator. Produces pseudo random numbers greater than or equal to zero and less than one. Based on a 24-bit Linear Congruential Generator. The 24-bit integers produced by the Linear Congruential Generator are normalized by dividing by the modulus, 2^24. Click on the button to the left for a description of the technique used to determine the Linear Congruential Generator constants.

The program accepts two optional parameters. The first is the number of pseudo random numbers to display. The default is 10. The second parameter is a seed value between zero and one, perhaps a value produced by the Rnd function. The default is a value based on the built in system timer. The program generates the subsequent values that would be produced by the Rnd function, thus duplicating the pseudo random sequence of numbers.

Because the Rnd function only displays the first 7 most significant digits of the pseudo random number, the value does not necessarily uniquely determine the subsequent sequence of numbers. There may be two possible sequences in which the given number can appear, in which case the program displays both series of numbers.

The Linear Congruential Generator produces pseudo random integers according to the following:

    Xi = [(A * Xi-1) + C] Mod M
Where:
    Xi-1 is the previous integer (seed value)
    Xi is the next integer
    A = 16,598,013
    C = 12,820,163
    M = 2^24 = 16,777,216 (the modulus)
This generator is maximal length with a period of 2^24 because:
    C is relatively prime to M.
    A - 1 is a multiple of every prime dividing M (2).
    A - 1 is a multiple of 4 if M is a multiple of 4.

The calculations involved to generate the pseudo random integers must be exact. There can be no round off errors. VBScript can only represent integers exactly to 53 bits. However, the term A * X-1 produces the largest intermediate result, and this term is a 48-bit number. The only part of the calculation that requires special treatment is the Mod function. The VBScript Mod function overflows when it operates on values greater than 2^31 - 1, so it cannot be used. The program calculates the Mod by dividing by M and determining the remainder. A special function is used to normalize values by dividing by M. Another function rounds the normalized values to the seven most significant digits, to duplicate the VBScript Rnd function.

Note that this generator did very poorly on the Diehard battery of tests of randomness (by George Marsaglia). This is to be expected of any 24-bit Linear Congruential Generator. The purpose of this program is to demonstrate how the VBScript Rnd function generates values. If you need a good source of random numbers, use a better generator, such as the Multiply With Carry or Combo Generators on this web site.

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