'aşağıdaki kısım modul içine yazılacak
Public Function RandomNumbers(Upper As Integer, _
Optional Lower As Integer = 1, _
Optional HowMany As Integer = 1, _
Optional Unique As Boolean = True) As Variant
On Error GoTo LocalError
If HowMany > ((Upper + 1) - (Lower - 1)) Then Exit Function
Dim x As Integer
Dim n As Integer
Dim arrNums() As Variant
Dim colNumbers As New Collection
ReDim arrNums(HowMany - 1)
With colNumbers
'First populate the collection
For x = Lower To Upper
.Add x
Next x
For x = 0 To HowMany - 1
n = RandomNumber(0, colNumbers.Count + 1)
arrNums(x) = colNumbers(n)
If Unique Then
colNumbers.Remove n
End If
Next x
End With
Set colNumbers = Nothing
RandomNumbers = arrNums
Exit Function
LocalError:
'Justin (just in case)
RandomNumbers = ""
End Function
Public Function RandomNumber(Upper As Integer, _
Lower As Integer) As Integer
On Error GoTo LocalError
'Generates a Random Number BETWEEN then LOWER
'and UPPER values
Randomize
RandomNumber = Int((Upper - Lower + 1) * Rnd + Lower)
Exit Function
LocalError:
RandomNumber = 1
End Function
'örnek uygulama: listeye 45 ile 1 arasında 10 tane rasgele rakam yazar
' true aynı sayı gelmemesi içindir
'boş bir forma bir listbox ve commondbutton ekleyip butonun
'eventine yazınız
List1.Clear
x = RandomNumbers(45, 1, 10, True)
For n = LBound(x) To UBound(x)
List1.AddItem x(n)
Next n
Print vbCrLf