B4J Code Snippet Random String Password

Code based on this thread...

I needed to generate a random password (string) of variable length that contains a mix of upper/lower case and numbers. It also avoids special characters.

It is random and there are no checks - so there are no guarantees that it would meet complexity rules.

Im sure there are better ways to do this but I just needed something quickly.

B4X:
Sub GenerateRandomPasswordString(Length As Int) As String
   Dim sb As StringBuilder
   sb.Initialize
   For i = 1 To Length
            Dim C As Int = Rnd(48, 122)
    
        Do While (C>= 58 And C<=64) Or (C>= 91 And C<=96)
             C = Rnd(48, 122)
        Loop
    
         sb.Append(Chr(C))
   Next
   Return sb.ToString
End Sub
 
Top