PRB: Same Seed for Randomize Does Not Repeat Previous Sequence

ID: Q129742


The information in this article applies to:
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0
  • Microsoft Visual Basic Standard and Professional Editions 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 query words: 3.00 4.00 vb4win vb4all

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: September 17, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.