PRB: Same Seed for Randomize Does Not Repeat Previous Sequence (129742)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 16-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 16-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0

This article was previously published under Q129742

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.

Modification Type:MinorLast Reviewed:1/8/2003
Keywords:kbprb KB129742