Android Question create random string (A-Z, 0-9) length = 9 [solved]

ronell

Well-Known Member
Licensed User
Longtime User
how to generate random string? see thread title

thanks in advance
 

stevel05

Expert
Licensed User
Longtime User
Try This:
B4X:
Public Sub GetRandomString(Source As String,Count As Int) As String
    Dim Str As StringBuilder
    Str.Initialize
    For i = 0 To Count -1
        Str.Append(Source.CharAt(Rnd(0,Source.Length)))
    Next
    Return Str.ToString
End Sub

Usage:

B4X:
Log(GetRandomString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",9))
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
How random do you want? You could also do something like use the Encryption library to produce one or more random numbers with the SecureRandom function. Use one of the message digest functions in the Encryption library to create a digest string, and then select nine characters from it, either using the function above, or simply picking a random start position for a substring.
 
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
thanks for the suggestion but im okay with @stevel05 code , dont want to add more libs in my project
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…