RANDOMIZE |
RANDOMIZE([number]) |
Initialises the random number generator. |
number | (Optional) The "seed" number to use (Variant) |
REMARKS |
* This function should be used in conjunction with the RND function. * If "number" is provided then this is used to initialize the random-number generator function (RND). * If "number" is left blank, then the number used is the value returned by the TIMER function. * To create the same sequence of random numbers every time, call RND with a negative value, RANDOMIZE with a numeric argument followed by subsequent calls to RND. * Once the RND function has been called the first time with a negative number, subsequent calls do not need a number passed in. * The equivalent .NET function is Microsoft.VisualBasic.VBMath.Randomize * For the Microsoft documentation refer to learn.microsoft.com |
'Use RND and RANDOMIZE to create the same sequence of random numbers every time
Debug.Print Rnd(-10) = 0.3276443
Call VBA.Randomize(10)
Debug.Print Rnd() = 0.2077829
Debug.Print Rnd() = 0.4407803
Debug.Print Rnd() = 0.6152623
'RND function with a negative number returns the same number every time
Debug.Print Rnd(-10) = 0.3276443
Debug.Print Rnd(-10) = 0.3276443
Debug.Print Rnd(-10) = 0.3276443
'RND function with a positive number returns a different number every time
Debug.Print Rnd(10) = volatile
Debug.Print Rnd(10) = volatile
Debug.Print Rnd(10) = volatile
'RND function with no number returns a different number every time
Debug.Print Rnd() = volatile
Debug.Print Rnd() = volatile
Debug.Print Rnd() = volatile
'The following 2 lines of code are identical
Call Randomize()
Call Randomize (Timer)
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top