RND

RND([number])

Returns a random number between 0 and 1 (Single).


numberThe number or any valid numerical expression (Single).

REMARKS
* This function should be used in conjunction with the RANDOMIZE statement.
* The value returned is less than 1 and greater than or equal to 0.
* The "number" determines how the random number is generated.
* If "number" < 0, then the same number is returned every time.
* If "number" > 0, then the next random number in the sequence is returned.
* If "number" = 0, then the most recently generated number is returned.
* If "number" is left blank, then the next random number in the sequence is returned.
* 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.
* To create random numbers in a given range, use the Excel user defined function RANDOMNUMBER.
* The equivalent Excel function is Application.WorksheetFunction.RAND
* The equivalent .NET function is [[Microsoft.VisualBasic.VBMath.Rnd]]
* 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

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top