Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB7
Expertise: beginner
Dec 1, 2003

GetRandomPassword - Generating a random password with the specified length

' Generate a random password with the specified length. The password will only 
' contain digits and letters (either lowercase or uppercase)
'
' Example: generating a password of 8 chars
'    Dim password As String = GetRandomPassword(8)

Function GetRandomPassword(ByVal length As Integer) As String
    Static rand As New Random
    Dim password As New System.Text.StringBuilder(length)

    For i As Integer = 1 To length
        Dim charIndex As Integer
        ' allow only digits and letters
        Do
            charIndex = rand.Next(48, 123)
        Loop Until (charIndex >= 48 AndAlso charIndex <= 57) OrElse (charIndex _
            >= 65 AndAlso charIndex <= 90) OrElse (charIndex >= 97 AndAlso _
            charIndex <= 122)
        ' add the random char to the password being built
        password.Append(Convert.ToChar(charIndex))
    Next
    Return password.ToString()
End Function
Marco Bellinaso
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date