Android Code Snippet Random Letter Generation

Sub Name: RandomLetters

Description: Returns a string containing random lower case, upper case and some special characters

Code:
B4X:
Sub RandomLetters(Length As Int) As String
    Dim SB As StringBuilder
    SB.Initialize
    For i = 1 To Length
        SB.Append(Chr(Rnd(65,123)))
    Next
    Return SB.ToString
End Sub

Dependencies: None


Tags: Random, Letters, Generation
 

sonicmayne

Member
Licensed User
Longtime User
Sub Name: RandomLowerCaseLetters

Description: Returns a string containing random lower case letters

Code:
B4X:
Sub RandomLowercaseLetters(Length As Int) As String
    Dim SB As StringBuilder
    SB.Initialize
    For i = 1 To Length
        SB.Append(Chr(Rnd(97,123)))
    Next
    Return SB.ToString
End Sub

Tags: Random, Letters, Generation
 

sonicmayne

Member
Licensed User
Longtime User
SubName: RandomUppercaseLetters

Description: Returns a string containing random uppercase letters

Code:
B4X:
Sub RandomUppercaseLetters(Length As Int) As String
    Dim SB As StringBuilder
    SB.Initialize
    For i = 1 To Length
        SB.Append(Chr(Rnd(65,91)))
    Next
    Return SB.ToString
End Sub

Tags: Random, Letters, Generation
 

DonManfred

Expert
Licensed User
Longtime User
Subname: RandomString

Description: Returns a string containing ramdom characters.

B4X:
Sub RandomString(Length As Int, LowerCase As Boolean, UpperCase As Boolean, Numbers As Boolean, AdditionalChars As String) As String
    Dim source As String
    If LowerCase = True Then
        source = source &"abcdefghijklmnopqrstuvwxyz"
    End If
    If UpperCase = True Then
        source = source &"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    End If
    If Numbers = True Then
        source = source &"0123456789"
    End If
    If AdditionalChars.Length > 0 Then
        source = source&AdditionalChars
    End If

    Dim SB As StringBuilder
  SB.Initialize
  For i = 1 To Length
    Dim r As Int = Rnd(0,source.Length)
        SB.Append(source.SubString2(r,r+1))
  Next
  Return SB.ToString
End Sub

Example:
B4X:
Log("RandomPW="&RandomString(25,True,True,True,"!§$%&="))
RandomPW=CTx21k5!1RaSF1IfuG&b5Dj7u

Tags: Random, Letters, Generation
 
Last edited:
Top