RND and RANDOMIZE Alternatives for Generating Random Numbers (28150)



The information in this article applies to:

  • Microsoft Visual Basic for MS-DOS
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft QuickBASIC 1.0
  • Microsoft QuickBASIC 1.01
  • Microsoft QuickBASIC 1.02
  • Microsoft QuickBASIC 1.0a
  • Microsoft QuickBASIC 1.0b
  • Microsoft QuickBASIC 2.0
  • Microsoft QuickBASIC 2.01
  • Microsoft QuickBASIC 3.0
  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft GW-BASIC 3.2
  • Microsoft GW-BASIC 3.22
  • Microsoft GW-BASIC 3.23
  • Microsoft QuickBASIC Compiler for the Apple Macintosh
  • Microsoft Basic Interpreter (BASIC86.EXE) for MS-DOS
  • Microsoft BASIC Interpreter for Apple Macintosh

This article was previously published under Q28150

SUMMARY

If you want a substitute for RND and RANDOMIZE, you can use your own equation to generate random numbers as shown below.

Microsoft Basic offers the RND function to return random single-precision numbers between 0.000000 and 1.000000. The RANDOMIZE statement can be used to reseed (or initially start) a given sequence returned by RND. Microsoft Basic uses the linear-congruential method for random-number generation in the RND function.

This information is also included with the Help file provided with the Standard and Professional Editions of Microsoft Visual Basic for MS-DOS, version 1.0.

MORE INFORMATION

Microsoft Basic uses the linear-congruential method for random-number generation in the RND function. The following is an example of the linear-congruential method formula, similar to that used by RND in Microsoft Basic:
   x1 = ( x0 * a + c ) MOD 2^24
				
In the above example, the variables equal the following:

x1=new number
x0=previous number
a=214013
c=2531011

(Note: the MOD operator in the formula above returns the integer remainder after an integer division.)

The expression x1/(2^24) returns a floating-point number between 0.0 and 1.0. Please refer to Code Examples 1 and 2 below for an illustration.

For more random number generation algorithms, see pages 353-364 of "Microsoft QuickBASIC Programmer's Toolbox," by John C. Craig, published by Microsoft Press (1988). Seven random number subprograms are documented, and a companion disk in MS-DOS format is also available from Microsoft Press.

The programs in Craig's book are written for Microsoft QuickBasic for MS-DOS, version 4.0 for the IBM PC. Some programs, such as the random number programs, are general and can easily be modified to run in Microsoft QuickBasic for the Apple Macintosh. When you run these programs, you may want to reseed the random number sequence regularly (such as every few hundred invocations) for greater uniformity.

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
				

Code Example 2

The following is the same as Example 1, except the random numbers are plotted to illustrate their uniform distribution:
' 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.
SCREEN 2
a = 214013
c = 2531011
z = 2 ^ 24
INPUT "Input seed value: ", x0
FOR count = 1 TO 5000
  temp = x0 * a + c
  ' Calculate (temp MOD z) and assign to x1:
  temp = temp / z
  x1 = (temp - FIX(temp)) * z
  result = x1 / z  ' Result is between 0.000000 and 1.000000
  GOSUB 100       ' Plot Result
  x0 = x1   ' x0 and x1 range from 0 to 16777216 (2^24)
NEXT
END
' Plot the random points to see their uniform distribution:
100 y = y + 1
    IF y > 200 THEN y = 0   ' Wrap plot at y=200 pixels.
    x = result * 500   ' Assumes screen mode <= 500 pixels wide.
    PSET (x, y)   ' PSET requires a graphics screen mode.
    RETURN
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB28150