Microsoft KB Archive/36736

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 17:55, 18 July 2020 by 3155ffGd (talk | contribs) (importing KB archive)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base


Article ID: 36736

Article Last Modified on 11/21/2006

This article was previously published under Q36736

SUMMARY

The first invocation of the RANDOMIZE statement determines a given set of random numbers returned from successive calls to the RND function. Not invoking the RANDOMIZE statement in a program is equivalent to invoking RANDOMIZE 0 before invoking RND. Note that a second (or subsequent) RANDOMIZE x statement does not restart the number sequence from the beginning of the set for a given x, but it randomly changes (reseeds) the sequence from what it would have been from that point on. This behavior is by design. Example 2 below illustrates this in detail.

If you want to return the same sequence of random numbers several times within a given program run, you can invoke the RND function with the exact same negative number argument followed by a sequence of RND invocations with a positive argument or no argument. Invoking RND with a negative argument eliminates the effect of a previous RANDOMIZE statement. Please see Example 1 below for further illustration.

MORE INFORMATION

This behavior of the RANDOMIZE statement and the RND function occurs in most versions of Microsoft Basic, including the following:

  • Microsoft QuickBasic versions 1.0, 1.01, 1.02, 2.0, 2.01, 3.0, 4.0, 4.0b, and 4.5 for the IBM PC
  • Microsoft Basic Compiler versions 5.35 and 5.36 for MS-DOS
  • Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS and MS OS/2
  • Microsoft Basic PDS versions 7.0 and 7.1 for MS-DOS and MS OS/2
  • Microsoft GW-Basic Interpreter versions 3.20, 3.22, and 3.23 for MS-DOS
  • Microsoft QuickBasic version 1.0 for the Apple Macintosh
  • Microsoft Basic Compiler version 1.0 for the Apple Macintosh
  • Microsoft Basic Interpreter versions 1.0, 1.01, 2.0, 2.1, and 3.0 for the Apple Macintosh
  • Microsoft Visual Basic programming system version 1.0 for Windows

Other versions of Basic may behave differently.

If you would like an alternative to this behavior of RND and RANDOMIZE, you may use your own formula to generate random numbers as shown in a separate article (query on the words RANDOM and EQUATION in this database for more information).

Example 1

(This example of invoking RND once with a negative argument always returns the same sequence of random numbers for subsequent invocations of RND.)

CLS
RANDOMIZE TIMER   ' Ignored unless you remove RND(-1) below.
FOR j = 1 TO 2
 ' Passing a negative value to the RND function supersedes the effect
 ' of the previous RANDOMIZE TIMER statement:
 PRINT RND(-1)   ' Remove this line for a different sequence on every
                 ' loop iteration. Otherwise, each j loop iteration
                 ' (and separate program run) returns the same
                 ' three-number sequence for the inner i loop.
 FOR i = 1 TO 3
    PRINT RND
 NEXT I
 PRINT
NEXT J
                

Example 2

FOR k = 1 TO 5
  PRINT RND
NEXT k
                

Below is the default random number set output from the above program, when run with QB.EXE version 4.0 on an IBM PC. The following sequence of random numbers varies with different versions of Microsoft QuickBasic and Basic Compilers:

.7107346
.99058
.8523988
.3503776
4.363585E-02


The following code shows the effect of RANDOMIZE 0 at start-up:

PRINT  "Set the seed to zero at startup"
RANDOMIZE 0
FOR k = 1 TO 5
  PRINT RND
NEXT k
PRINT "Again, reset the seed to zero"
RANDOMIZE 0
FOR k = 1 TO 5
  PRINT RND
NEXT k
                

The above program has the following output:

   Set the seed to zero at startup
    .7107346
    .99058
    .8523988
    .3503776
    4.363585E-02
   Again, reset the seed to zero
    .7987763
    .6497337
    .5426014
    .9642789
    8.590406E-02
                

Note: The second invocation of RANDOMIZE 0 does not restart the sequence from the beginning. This is by design. If you remove the second RANDOMIZE 0 statement and run the program again, the sixth through tenth numbers are different than above. This shows that multiple RANDOMIZE statements reseed the sequence (and change the random number set displayed), but they do not restart the sequence from the beginning.


Additional query words: QuickBas BasicCom MQuickB 1.00 2.00 2.10 3.00 3.20 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Keywords: KB36736