Metoda Random (Acak)

Kode sumber berikut ini adalah metoda untuk mengacak angka tertentu (misal: dari 1 sampai 100), tanpa ada pengulangan angka yang sudah ditampilkan.

Dim intHimpunan() As Integer

Function AcakPosisi(BatasAwal&, BatasAkhir&) As String
    Dim k, intRange As Integer
    Dim i, intRndUlang As Integer
    
    intRange = BatasAkhir - BatasAwal + 1
    
    ReDim Preserve intHimpunan(1 To intRange) As Integer
    
    For k = 1 To intRange
        Randomize
        intRndUlang = AcakUlang(BatasAwal, BatasAkhir)
                                   
        For i = 1 To k - 1
            Do Until intHimpunan(i)  intRndUlang
                If intHimpunan(i) = intRndUlang Then
                    intRndUlang = AcakUlang(BatasAwal, BatasAkhir)
                    i = 1
                End If
            Loop
        Next i
      
        intHimpunan(k) = intRndUlang
      
        AcakPosisi = AcakPosisi & intRndUlang & ";"
    Next k
End Function

Private Function AcakUlang(BatasAwal, BatasAkhir)
  AcakUlang = Int((BatasAkhir - BatasAwal + 1) * Rnd + BatasAwal)
End Function

Private Sub cmdTest_Click()
Dim Himpunan$
    Himpunan = AcakPosisi(1, 100)
    Msgbox Himpunan 
End Sub

Perihal Novian Agung
Pengangguran

2 Responses to Metoda Random (Acak)

  1. Free Koneksi mengatakan:

    Thank’s for the knowledge
    Success for you

  2. Budi Santoso mengatakan:

    kalau datanya dari database bro??

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: