Microsoft KB Archive/129742

= PRB: Same Seed for Randomize Does Not Repeat Previous Sequence =

Article ID: 129742

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



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
 Start a new project in Visual Basic. Form1 is created by default.  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  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. 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: kbprb KB129742

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.