vbdpss.hlp (Table of Contents; Topic list)
Article Q28150
                                                 Contents  Index  Back
─────────────────────────────────────────────────────────────────────────────
                           Knowledge Base Contents  Knowledge Base Index
 
 Alternatives to RND and RANDOMIZE for Generating Random Numbers - Q28150
 
 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.
 
 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.
 
    See Example 1
    See Example 2
 
 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 QuickBasic 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.