Random password generator function using VB.NET

Random password generator function using VB.NET

During an application development many time we required a random string. Whether it is to make the URL unique or to send a SMS for mobile verification or to generate dynamic Cookie random strings are very useful. In the below Random password generator function I am doing so using VB.NET String Builder methods from System.IO Namespace.

Logic I implemented here is so simple. Inside a for loop I am using Convert.ToChar() method against random.NextDouble() Function. Random function is taking care to generate random number. While using Convert.ToChar() I am converting the random number to String and appending that to a StringBuilder variable.

VB.NET Random password generator

Private Function RandomString(ByVal size As Integer, ByVal lowerCase As Boolean) As String
Dim builder As New StringBuilder()
Dim random As New Random()
Dim ch As Char
Dim i As Integer
For i = 0 To size - 1
ch = Convert.ToChar(Convert.ToInt32((26 * random.NextDouble() + 65)))
builder.Append(ch)
Next i
If lowerCase Then
Return builder.ToString().ToLower()
End If
Return builder.ToString()
End Function