Function Random Values Class

RandomLongGenerator

"Random Long Number Generator" Generate a series of random numbers with OR without replacement and store them in a Variant array. Random numbers will be distributed evenly within the range. Returns True upon success and False upon failure.

Signature (Visual Basic / VBA)

Public Function RandomLongGenerator( _
      ByRef rNumbers() As Variant _
    , Optional ByVal vLower As Variant _
    , Optional ByVal vUpper As Variant _
    , Optional ByVal vNumRandomNums As Variant _
    , Optional ByVal vWithReplacement As Variant _
    , Optional ByVal vDoReDim As Variant _
    ) As Boolean

Code Example

    Assuming
       Dim avarNums() As Variant
       Rnd(-1) = 0.224007
    for example
       RandomLongGenerator(avarNums(), 1, 5, 5, False, True) = True
       VariantVectorToString(avarNums(), " ") = "1 3 4 5 2"
    leaves
       LBound(avarNums) = 0
       UBound(avarNums) = 5
       IsEmpty(avarNums(0)) = True
       avarNums(1) = 1
       avarNums(2) = 3
       avarNums(3) = 4
       avarNums(4) = 5
       avarNums(5) = 2
    Also
       RandomLongGenerator(avarNums(), 1, 3, 5, True, True) = True
       VariantVectorToString(avarNums(), " ") = "3 1 1 2 1"

Parameters

Parameter Description
rNumbers The one-dimensional array of Variants in which the random numbers will be stored. Random numbers are stored within elements 1 through vNumRandomNums within this array. This array can be either static, fixed, or dynamic; see the vDoReDim argument. The lower bound of the array will be 0 (zero) if vDoReDim is True.
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 False if vLower > (is greater than) vUpper.
vNumRandomNums The number of random numbers that are to be generated. vNumRandomNums defaults to 1 (one) if it is missing or Null or cannot be fixed up to a number.
vWithReplacement True if duplicate random numbers are allowed within the list of those generated, or False if the random numbers are to be unique. vWithReplacement defaults to False if it is missing or Null or cannot be fixed up to a number.
vDoReDim True if rNumbers is a dynamic array of Variants which this function (RandomLongGenerator) should re-dimension before assigning values into it, or False if rNumbers is a static, fixed, or pre-dimensioned dynamic array of Variants. vDoReDim defaults to True if it is missing or Null or cannot be fixed up to a number.

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 This function uses the RandomLongWithinRange function to generate each of the candidate random numbers.
Note Function returns False if vWithReplacement is False and vNumRandomNums is greater than the range of allowable values. For example, the function cannot generate 10 unique random Long (integer) numbers whose value is between 1 and 5.