Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
Article Q28150, Example 1
◄Contents► ◄Index► ◄Back►
─────────────────────────────────────────────────────────────────────────────
◄Knowledge Base Contents► ◄Knowledge Base Index►
Alternatives to RND and RANDOMIZE for Generating Random Numbers, Example 1
Code Example 1
--------------
The following is an example of the linear congruential method for
generating pseudo-random numbers:
' To try this example in VBDOS.EXE:
' 1. From the File menu, choose New Project.
' 2. Copy the code example to the Code window.
' 3. Press F5 to run the program.
DEFDBL A-Z ' Requires double-precision intermediate variables.
a = 214013
c = 2531011
z = 2 ^ 24
INPUT "Input any seed value: ", x0
FOR count = 1 TO 25 ' print 25 random numbers between 0.0 and 1.0:
temp = x0 * a + c
' Calculate (temp MOD z) and assign to x1:
temp = temp / z
x1 = (temp - FIX(temp)) * z
' Print the result as value between 0.0000000 and 1.0000000:
result = x1 / z
PRINT result
' Reseed the calculation before the next iteration:
x0 = x1 ' x0 and x1 range from 0 to 16777216 (2^24)
NEXT