View Single Post
 
Old 02-27-2012, 06:51 AM
tinfanide tinfanide is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Code:
Sub test()

Dim x As Integer

For x = 0 To 3
    MsgBox (ReturnRandomNumber(0, 3, x))
Next x

End Sub

Public Function ReturnRandomNumber(lower As Integer, upper As Integer, turn As Integer) As String
    
    Dim n As Integer, arr() As Variant
    
Redo:
    n = Int((upper - lower + 1) * Rnd + lower)
    Debug.Print (n)
    
    ReDim Preserve arr(turn)
    
    For a = lower To upper
        If arr(a) <> "" Then
            If arr(a) = n Then GoTo Redo
        Else
            Exit For
        End If
    Next a
    
    arr(turn) = n
    ReturnRandomNumber = n
    
End Function
But the array is still empty every the function restarts. It fails when I want the array that stores every n to check if n is duplicated.
Reply With Quote