Function Random Values Class

RandomDoubleWithinRange

"Random Double Within Range " Returns a random Double number between vLower and vUpper inclusive. Random numbers will be distributed evenly within the range.

Signature (Visual Basic / VBA)

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

Code Examples

    ? Rnd(-1)
     0.224007
    ? RandomDoubleWithinRange(0, 10)
     1.64263945772881
    ? RandomDoubleWithinRange(0, 10)
     5.10665292422705
    ? RandomDoubleWithinRange(0, 10)
     6.70221094252112
    ? RandomDoubleWithinRange(-100, 100)
    -81.9635057024926
    ? RandomDoubleWithinRange(-100, 100)
     81.7261980739441
    ? RandomDoubleWithinRange(-100, 100)
     24.5903145544456
    ? RandomDoubleWithinRange()
     2.97827178053297E+286
    ? RandomDoubleWithinRange()
     7.22218871117141E-113
    ? RandomDoubleWithinRange()
    -6.08308655657523E+174
    dblRnd * (vUpper - vLower) + vLower

Parameters

Parameter Description
vLower The lower limit of the range in which the generated random number will fall. vLower defaults to MinDouble 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 MaxDouble 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.
vMaxTries The maximum number of tries to generate a random number within the specified range. vMaxTries defaults to 128 if it is missing or Null or cannot be fixed up to a number. Function returns Null if it still cannot generate a random number within the specified range using the alternate method described in the summary.

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 repeatedly generate random values until it gets one within the specified range, OR it has tried vMaxTries times.
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.