XL: Randomize Statement Doesn't Re-initialize Rnd Function

Last reviewed: May 30, 1997
Article ID: Q120587
5.00 5.00c 7.00 WINDOWS kbprg kbdocerr

The information in this article applies to:

  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0

SYMPTOMS

In Microsoft Excel, when you use the Randomize statement in a Visual Basic procedure to initialize the random-number generator, the Rnd function returns a different series of random numbers each time you use it in the procedure, even if you use the Randomize statement with the same number value before each Rnd function.

CAUSE

The Visual Basic documentation for the Randomize statement states that the Randomize statement uses the number argument to initialize a random-number generator, giving it a new seed value.

This information implies that the Randomize statement re-initializes the Rnd function, so that when you use the Randomize statement with the same number value preceding each Rnd function in a procedure, each Rnd function returns the same pattern of random numbers.

However, this is incorrect. The current functionality of the Randomize statement in Visual Basic, Applications Edition, is the same as in the Basic, Visual Basic, and QuickBasic languages. The Randomize statement does not reset the seed for the Rnd function.

WORKAROUND

To re-initialize the random-number generator, use the Rnd function with a value of -1 to re-initialize the Rnd function, and then use the Randomize statement with the value you want to use as the seed value for the Rnd function. The following is an example of using this method:

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose.

   Sub Random_Numbers()
       ' Define variable i as Integer type
       Dim i As Integer
       Workbooks.Add

       ' Initialize the random-number generator
       Rnd (-1)
       Randomize 10

       ' Insert random numbers in column A
       For i = 1 To 100
           Cells(i, 1) = Rnd
       Next

       ' Re-initialize the random-number generator
       Rnd (-1)
       Randomize 10

       ' Insert same series of random numbers in column B
       For i = 1 To 100
           Cells(i, 2) = Rnd
       Next

   End Sub

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

You can use the Randomize statement to create a seed value for the Rnd function, or initialize the Rnd function. If Randomize is not used, the same initial seed is always used to start the sequence.

REFERENCES

For more information about the Randomize Statement, choose the Search button in the Visual Basic Reference and type:

    Randomize

For more information about the Rnd Function, choose the Search button in the Visual Basic Reference and type:

    Rnd


KBCategory: kbprg kbcode
KBSubcategory: kbdocerr

Additional reference words: 1.00 5.00 5.00c 7.00
Keywords : kbdocerr kbcode kbprg
Version : 5.00 5.00c 7.00
Platform : WINDOWS


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: May 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.