<-- Previous || Up || Next -->

Random Integer Within Range Verify Sub
Random Values Class

Private Sub RandomIntegerWithinRangeVerify()
    'Check to see how many of the valid integers are generated by our random integer generator.
    'Note that this function does not check to see if the random number 0 (zero) is generated.

    'See also:
    '  RandomLongWithinRangeVerify Subroutine
    '  RndBitsVerify Subroutine, et. al.

    Const lngMaxTries = 24

    Dim alngNeg() As Long
    Dim alngPos() As Long
    ReDim alngNeg(MinInteger To -1)
    ReDim alngPos(1 To MaxInteger)

    Dim intPass As Integer
    For intPass = 1 To lngMaxTries
        Debug.Print "Pass #" & intPass & "..."

        ' Generate and log the random numbers.

        Dim lngTry As Long
        For lngTry = 1 To CLng(MaxInteger) - CLng(MinInteger)
            Dim varRand As Variant
            varRand = RandomIntegerWithinRange(MinInteger, MaxInteger)

            If IsNull(varRand) Then
                Debug.Print "Error: Try #" & lngTry & ": Result is Null"
            ElseIf varRand < 0 Then
                alngNeg(varRand) = alngNeg(varRand) + 1
            ElseIf varRand > 0 Then
                alngPos(varRand) = alngPos(varRand) + 1
            End If
        Next lngTry

        ' Find number of random numbers not used.
    
        Dim lngUnused As Long
        lngUnused = 0
    
        Dim lngElem As Long
        For lngElem = LBound(alngNeg) To UBound(alngNeg)
            If alngNeg(lngElem) = 0 Then lngUnused = lngUnused + 1
        Next lngElem
        For lngElem = LBound(alngPos) To UBound(alngPos)
            If alngPos(lngElem) = 0 Then lngUnused = lngUnused + 1
        Next lngElem
    
        Dim dblPctUnused As Double
        dblPctUnused = CDbl(lngUnused) / (CDbl(MaxInteger) - CDbl(MinInteger)) * 100#
        Debug.Print "Unused: " & Format$(lngUnused, "#,##0") & " (" & Format$(dblPctUnused, "#,##0.000 000") & "%)"

        ' Find minimum random number generated.
    
        Dim varMin As Variant
        varMin = "(Does Not Exist)"
    
        For lngElem = LBound(alngNeg) To UBound(alngNeg)
            If alngNeg(lngElem) <> 0 Then
                varMin = lngElem
                Exit For
            End If
        Next lngElem
    
        Debug.Print "Minimum: " & varMin & " (Min Integer = " & MinInteger & ")"
    
        ' Find maximum random number generated.
    
        Dim varMax As Variant
        varMax = "(Does Not Exist)"
    
        For lngElem = UBound(alngPos) To LBound(alngPos) Step -1
            If alngPos(lngElem) <> 0 Then
                varMax = lngElem
                Exit For
            End If
        Next lngElem
    
        Debug.Print "Maximum: " & varMax & " (Max Integer = " & MaxInteger & ")"

        If lngUnused = 0 Then Exit For
    Next intPass
End Sub

Copyright 1996-1999 Entisoft
Entisoft Tools is a trademark of Entisoft.