Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleGenerate random text
Keywordscode, random text
CategoriesStrings, Tips and Tricks
 
Use Randomize to initialize the random number generator. Then for each letter, use Rnd to pick a number between 0 and 26 + 26 + 10. If the number is less than 26, use it to pick a letter between A and Z. If the number is between 25 and 2 * 26, subtract 26 and use the result to pick a number between a and z. If the number is 2 * 26 or greater, subtract 2 * 26 and use the result to pick a digit between 0 and 9.
 
Private Sub Command1_Click()
    Dim num_characters As Integer
    Dim i As Integer
    Dim txt As String
    Dim ch As Integer
    
        Randomize
        num_characters = CInt(txtNumCharacters.Text)
        For i = 1 To num_characters
            ch = Int((26 + 26 + 10) * Rnd)
            If ch < 26 Then
                txt = txt & Chr$(ch + Asc("A"))
            ElseIf ch < 2 * 26 Then
                ch = ch - 26
                txt = txt & Chr$(ch + Asc("a"))
            Else
                ch = ch - 26 - 26
                txt = txt & Chr$(ch + Asc("0"))
            End If
        Next i
    
        txtResults.Text = txt
    End Sub
 

Formatted by Neil Crosby

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated