Create random string

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
I want to create 1000 variables casual strings.
Please, who help me?
Thk
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
all upper and lower. Also special character like ! ? ^ - &
 
Upvote 0

Fusseldieb

Active Member
Licensed User
Longtime User
For instance, if someone wants to generate a A-Z, a-z and 0-9 string with X characters, they can use this approach below:

B4X:
Sub GenerateRandomString(StrLength As Int) As String
    Dim RndString As String
    Dim RndNumber As Int
    Do While RndString.Length < StrLength
        RndNumber = Rnd(48,123) 'Yep, 123, because the last number is "exclusive"
        If (RndNumber >= 48 And RndNumber <= 57) Or (RndNumber >= 65 And RndNumber <= 90) Or (RndNumber >= 97 And RndNumber <= 112) Then
            RndString = RndString & Chr(RndNumber)
        End If
    Loop
    Return RndString
End Sub

Use it this way:
B4X:
GenerateRandomString(16) 'This will generate 16 random characters

'Outputs something like this: Phapk3eN6VmlbGlU

Explanation:
48-57 are 0-9
65-90 are A-Z
97-122 are a-z
Anything in between we jump with the "If" statement
Character Codes Reference: http://www.theasciicode.com.ar/ascii-printable-characters/

(Yeah, this thread is 4 years old, but just contributing to the "wiki" here if anyone search for that on Google, because I didn't find anything similar in B4A)
 
Last edited:
Upvote 0
Top