PRB: Same Seed for Randomize Does Not Repeat Previous Sequence

Last reviewed: October 30, 1995
Article ID: Q129742
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, version 3.0

SYMPTOMS

When you use the Randomize function with the same value for the seed number, the function does not repeat the previous sequence.

WORKAROUND

To repeat sequences of random numbers, call the Rnd function with a negative argument immediately before using Randomize with a numeric argument.

STATUS

This behavior is by design.

MORE INFORMATION

The Randomize function uses the number specified as its parameter to initialize the Rnd function's random-number generator, giving it a new seed value. This implies that if Randomize is called with the same seed number, it should reinitalize Rnd, so that subsequent calls will generate the same pattern of numbers.

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add the following code to the Form1_Click event procedure:

    For m = 1 To 3

          'Rnd(-1)
          Randomize 100
          For n = 1 To 5
    
             Debug.Print Str$(m) & "." & Str$(n) & ":"; Rnd
          Next
       Next
    
    

  3. Press the F5 key to run the program. Click Form1. The Debug Window should display three different sets of numbers for each value of the variable m.

  4. Turn the 'Rnd(-1) commented line into an executed line, and rerun the program. The Debug window now displays the same set of numbers for each value of the variable m.


Additional reference words: 3.00 4.00 vb4win vb4all
KBCategory: kbprg kbprb
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 30, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.