Function Random Values Class

RandomLongWithinRange

Returns a random Long (integer) number between vLower and vUpper inclusive. Random numbers will be distributed evenly within the range.

Signature (Visual Basic / VBA)

Public Function RandomLongWithinRange( _
      Optional ByVal vLower As Variant _
    , Optional ByVal vUpper As Variant _
    ) As Variant

Code Examples

    Rnd(-1) = 0.224007
    RandomLongWithinRange() = -1441975373
    RandomLongWithinRange() = 45807082
    RandomLongWithinRange() = 731094033
    RandomLongWithinRange(1) = 193665383
    RandomLongWithinRange(1) = 1951270193
    RandomLongWithinRange(1) = 1337778316
    RandomLongWithinRange(1, 100000) = 71296
    RandomLongWithinRange(1, 100000) = 67593
    RandomLongWithinRange(1, 100000) = 87716
    CLng(Int(dblRnd * (1 + vUpper - vLower) + vLower))

Parameters

Parameter Description
vLower The lower limit of the range in which the generated random number will fall. vLower defaults to MinLong 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 MaxLong 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.

Notes & Version History

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.
Algorithm Before using the Visual Basic Help method for generating numbers within a range, this method uses the random parts of the Single value returned by the built-in Rnd function to construct a fairly-random Double precision value to which the range function is applied. This is done to avoid the problem of having certain long values which can never possibly get generated because the precision of the individual Single precision random value returned by VB does not contain enough precision to guarantee it.