Signature (Visual Basic / VBA)
Public Function RandomIntegerWithinExpRange( _
Optional ByVal vLower As Variant _
, Optional ByVal vUpper As Variant _
, Optional ByVal vBase As Variant _
) As Variant
Code Examples
Rnd(-1) = 0.224007
RandomIntegerWithinExpRange(1, 64, 2) = 1
RandomIntegerWithinExpRange(1, 64, 2) = 1
RandomIntegerWithinExpRange(1, 64, 2) = 2
RandomIntegerWithinExpRange(1, 1000, 3) = 6
RandomIntegerWithinExpRange(1, 1000, 3) = 189
RandomIntegerWithinExpRange(1, 1000, 3) = 134
CInt(Int((Rnd * (1 + vUpper - vLower) ^ (1 / vBase)) ^ vBase + vLower))
Parameters
| Parameter | Description |
|---|---|
| vLower | The lower limit of the range in which the generated random number will fall. vLower defaults to MinInteger if it is missing or Null or cannot be fixed up to a number. |
| vUpper | The upper limit of the range in which the generated random number will fall. vUpper defaults to MaxInteger if it is missing or Null or cannot be fixed up to a number. Function returns Null if vLower > (is greater than) vUpper. Function returns vLower if vLower = (equals) vUpper. |
| vBase | See the definition below. vBase defaults to 1 (one) if it is missing or Null or cannot be fixed up to a number. With a vBase of 1 (one), this is the same as calling the RandomIntegerWithinRange function. |
Notes & Version History
| Summary | The function first tries to generate a random number using the definition below. If that calculation causes an Overflow or other type of error, the function will repeat it up to 32 times. Function returns Null if it still cannot generate a random number after 32 attempts. |
| Note | The call to the Visual Basic Rnd function in the above example seeds the Visual Basic random number generator so that the sample results can be reproduced. There is generally no need to call the Rnd function within your programs since Entisoft Tools initializes the Visual Basic random number generator once with "Randomize" statement. |