devxlogo

GetRandomPassword – Generating a random password with the specified length

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

See also  The Importance of Data Security in Hospitality
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist