Microsoft KB Archive/28150

= RND and RANDOMIZE Alternatives for Generating Random Numbers =

Article ID: 28150

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft QuickBasic 1.0
 * Microsoft QuickBasic 1.01
 * Microsoft QuickBasic 1.02
 * Microsoft QuickBASIC 1.0b
 * 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 for MS-DOS
 * Microsoft GW-BASIC 3.2
 * Microsoft GW-BASIC 3.22
 * Microsoft GW-BASIC 3.23
 * Microsoft QuickBasic Compiler for Macintosh 1.0
 * Microsoft BASIC Interpreter 7.0
 * Microsoft BASIC Interpreter 3.0 for 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 &quot;Microsoft QuickBASIC Programmer's Toolbox,&quot; 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 &quot;Input any seed value: &quot;, 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 &quot;Input seed value: &quot;, 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

Additional query words: VBmsdos QuickBas BasicCom MQuickB

Keywords: KB28150

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.