Home > Visual Basic 6 > Membuat Fungsi Random Huruf Dan Angka Pada Visual Basic

Membuat Fungsi Random Huruf Dan Angka Pada Visual Basic

Source code berikut untuk membuat fungsi acak (random) huruf dan angka pada Visual Basic 6. Panjang karakter yang di random sebanyak 10 karakter.

Buat 1 project dengan :
1 Form
1 CommandButton
1 TextBox

Copy source code berikut pada Form : 

Private Sub Command1_Click()
    Dim A As String
    Dim B As String
    Dim C As String
    Dim D As String
    Dim E As String
    Dim F As String
    Dim G As String
    Dim H As String
    Dim I As String
    Dim J As String

    A = Random
    B = Random
    C = Random
    D = Random
    E = Random
    F = Random
    G = Random
    H = Random
    I = Random
    J = Random

    Text1 = A + B + C + D + E + F + G + H + I + J
End Sub

Function RandomNum() As Integer
    RandomNum = Int((9 - 1 + 1) * Rnd + 1)
End Function

Function RandomChar() As String
    Dim Char As Integer
    Char = Int((26 - 1 + 1) * Rnd + 1)
    If Char = 1 Then RandomChar = "A": Exit Function
    If Char = 2 Then RandomChar = "B": Exit Function
    If Char = 3 Then RandomChar = "C": Exit Function
    If Char = 4 Then RandomChar = "D": Exit Function
    If Char = 5 Then RandomChar = "E": Exit Function
    If Char = 6 Then RandomChar = "F": Exit Function
    If Char = 7 Then RandomChar = "G": Exit Function
    If Char = 8 Then RandomChar = "H": Exit Function
    If Char = 9 Then RandomChar = "I": Exit Function
    If Char = 10 Then RandomChar = "J": Exit Function
    If Char = 11 Then RandomChar = "K": Exit Function
    If Char = 12 Then RandomChar = "L": Exit Function
    If Char = 13 Then RandomChar = "M": Exit Function
    If Char = 14 Then RandomChar = "N": Exit Function
    If Char = 15 Then RandomChar = "O": Exit Function
    If Char = 16 Then RandomChar = "P": Exit Function
    If Char = 17 Then RandomChar = "Q": Exit Function
    If Char = 18 Then RandomChar = "R": Exit Function
    If Char = 19 Then RandomChar = "S": Exit Function
    If Char = 20 Then RandomChar = "T": Exit Function
    If Char = 21 Then RandomChar = "U": Exit Function
    If Char = 22 Then RandomChar = "V": Exit Function
    If Char = 23 Then RandomChar = "W": Exit Function
    If Char = 24 Then RandomChar = "X": Exit Function
    If Char = 25 Then RandomChar = "Y": Exit Function
    If Char = 26 Then RandomChar = "Z": Exit Function
End Function

Function Random() As Variant
    Dim Randm As Integer
    Randm = Int((3 - 1 + 1) * Rnd + 1)
    If Randm = 1 Then
        Random = RandomNum
    Else
        Random = RandomChar
    End If
End Function

Untuk lebih jelasnya download source code :


Referensi :
http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=23378&lngWId=1

  1. Benni
    April 22, 2010 at 3:10 pm

    kalo gini ga random beneran..

  2. April 22, 2010 at 9:04 pm

    silahkan coba dulu..

  3. Rianruli
    October 21, 2010 at 3:50 am

    kalo mau random caption di label gimana,bro? cuma dua jenis.caption’nya misalnya di ganti YA – TIDAK tp secara acak.thanks.

  4. han
    January 3, 2011 at 7:09 pm

    mas kalo dipakai di delphi 7 gimana, bisa gak

  5. February 11, 2011 at 10:25 am

    Hm…….. makasih bangets kawan, aq terbantu bangets ma materi engkau publikasikan’
    klo misalx mau ngacak data pada saat form Load pd vb gimana ya..?
    yg pd saat pertamakali tampil data acakx selalu berbeda.

  6. March 4, 2011 at 11:58 pm

    Ini alternatif kode yg lbh singkat :

    Function Random() As String
    Randomize
    Random = IIf(CInt(Rnd * 1) = 0, Chr(CInt((Rnd * 25) + 97)), Chr(CInt((Rnd * 9) + 48)))
    If CInt(Rnd * 1) = 0 Then Random = UCase(Random)
    End Function

    • March 5, 2011 at 9:13 pm

      thanks for sharing..

      • satria
        July 24, 2011 at 6:52 pm

        seumpama yg di random/di acak adalah kata dari sebuah inputan dari inputbox yg jumblah inputannya di tentukan oleh si pemakaii program gmn ya???

        misalnya aku inputkan 3 kata yaitu asa,asi,sas gimana kode nya om…
        mohon bantuannya…

  7. Jemmy
    February 14, 2012 at 9:19 pm

    Ini kan hurup, bisa tidah kalo yang dirandom itu gambar ?

  8. January 23, 2013 at 11:35 pm

    oh begitu ya,,,klo boleh nanya? sama gak dengan random charackter

  9. nurjaman
    June 30, 2014 at 9:46 pm

    Mas kalo Versi VB.Net 2008 gimana ya? kok yang ada malah exenya yang error?

  10. Yondaime
    April 23, 2016 at 9:05 pm

    Bisa gan, lumayan membantu utk ngembangin project saya, diedit dikit sama saya jadi cuma 2 max lengthnya sama integer only. thanks…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: