Monday, May 28, 2012

Generate nomor Secara Unik

Di bawah ini merupakan fungsi yang berlakuk sebagai sebuah generator agar menampilkan nomor secara unik (tidak ada yang sama satu dengan yang lainnya).
Option Explicit 

Private Function
GenRanUnix(MIN As Integer, MAX As Integer) As Collection

Dim
iMax As Integer
Dim
iRan As Integer
Dim g As Integer
Dim y As Integer
Dim c As New Collection
Dim k As New Collection
Dim f As Integer
Dim x As Integer

For f =
MIN To MAX
c.Add f
Next

y =
c.Count
Randomize

For x =
1 To y
g =
Int(y * Rnd + 1)
k.Add c.Item(g)
c.Remove g
y =
c.Count
Next

Set
GenRanUnix = k

End Function

Contoh penggunaan generate nomor secara unik
Private Sub Command1_Click() 
Dim b As New Collection
Dim i As Integer
Dim
msg As String
List1.Clear
Set b = GenRanUnix(0, 100)
For i = 1 To b.Count
List1.AddItem b.Item(i)
Next
End Sub