weAscend.com

 

Function fRand(intStart As Integer, intEnd As Integer, _
intReturn As Integer, fUnmatched As Boolean, _

ParamArray aExclude()) As String

'*********************************
'Author: Michael Blake
'Contact Via: www.weAscend.com
'Date: 07/09/2000
'
'Example:
'fRand(3,24,5,True,True,5,8,13)
'
'3 = The starting number of the range
'24 = The ending number of the range
'5 = The number of integers to return.
'True = There will be no duplicate numbers
'True = There will be numbers excluded. _

        (This only works if fUnmatched = True)
'5,8,13 = Numbers to exclude from result
'
'Notes: If fUnmatched is True then intReturn is
' limited to the number of integers
' between intStart And intEnd.
' If fUnmatched is False then intReturn is
' unlimited.
'
'Please include this information when using
'this function. - Thank you
'********************************

Dim aintExclude() As Boolean
Dim i As Integer
Dim intCount As Integer
Dim intTemp As Integer
Dim strDelim As String
Dim Count As Integer

On Error GoTo Error_Handler

ReDim aintExclude(intStart To intEnd)

If aExclude(0) Then
   For Count = 1 To UBound(aExclude)
      aintExclude(aExclude(Count)) = True
   Next Count
Else
   Count = 0
End If

If (intEnd - intStart - (Count)) < (intReturn) And _
fUnmatched = True Then GoTo Error_Handler

fRand = ""

strDelim = ", "
i = 1
intCount = 1

If fUnmatched = True Then
For i = 1 To intReturn
Start_Again:
   Randomize
   intTemp = (Int((intEnd - (intStart - 1)) * Rnd()) + 1) + (intStart - 1)
   If aintExclude(intTemp) Then GoTo Start_Again
   aintExclude(intTemp) = True
   fRand = fRand & intTemp & strDelim
Next i
Else
   For i = 1 To intReturn
      Randomize
      intTemp = (Int((intEnd - (intStart - 1)) * Rnd()) + 1) + (intStart - 1)
      fRand = fRand & intTemp & strDelim
   Next i
End If

fRand = Left(fRand, Len(fRand) - Len(strDelim))

Exit_Function:
Exit Function

Error_Handler:
fRand = "#Error#"
GoTo Exit_Function

End Function

 

Contact Us