Get a random String

ilan

Expert
Licensed User
Longtime User
you can use this to generate a random userid:

function:
B4X:
Sub RandomString(length As Int) As String
    Dim abc As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    Dim randomstr As String = ""
    For i = 0 To length - 1
        randomstr = randomstr & (abc.CharAt(Rnd(0,abc.Length)))
    Next
    Return randomstr
End Sub

use:
B4X:
Log(RandomString(12))
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Args() As String)
   Dim n As Long = DateTime.Now
   RandomString(30000)
   Log(DateTime.Now - n) '583 milliseconds
   Dim n As Long = DateTime.Now
   RandomString2(30000)
   Log(DateTime.Now - n) '5 milliseconds
End Sub

Sub RandomString(length As Int) As String
   Dim abc As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
   Dim RandomStr As String = ""
   For i = 0 To length - 1
     RandomStr = RandomStr & (abc.CharAt(Rnd(0,abc.Length)))
   Next
   Return RandomStr
End Sub

Sub RandomString2(length As Int) As String
   Dim abc As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
   Dim RandomStr As StringBuilder
   RandomStr.Initialize
   For i = 0 To length - 1
     RandomStr.Append(abc.CharAt(Rnd(0,abc.Length)))
   Next
   Return RandomStr.ToString
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Strings are immutable objects. This means that for each modification a new string is created.
Consider this code:
B4X:
Dim s As String = "1000 characters long string..."
s = s & "."
In the second line a new string is created with a size of 1001 characters. The 1000 characters from the previous string are copied to the new string and a dot is added as the last character.

This happens on each iteration. For short strings the overhead is negligible, however for longer strings it becomes huge.

StringBuilder on the other hand is a mutable string.
B4X:
sb.Append(".")
It maintains an internal buffer and grows it as needed (with enough room for many more characters).
So appending a character is a very quick operation. It just copies the character to the internal buffer.
 

Derek Johnson

Active Member
Licensed User
Longtime User
If users have to type in their userid you might want to consider whether it needs to have both upper case and lower case letters. Many systems use userids that are not case sensitive, although passwords are almost always case sensitive.
 

ilan

Expert
Licensed User
Longtime User
If users have to type in their userid you might want to consider whether it needs to have both upper case and lower case letters. Many systems use userids that are not case sensitive, although passwords are almost always case sensitive.

you could use:

B4X:
Return randomstr.ToLowerCase

for username ;)
 

Derek Johnson

Active Member
Licensed User
Longtime User
you could use:

B4X:
Return randomstr.ToLowerCase

for username ;)

or just take the set of uppercase letters out of this string:

B4X:
   Dim abc As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

B4X:
   Dim abc As String = "0123456789abcdefghijklmnopqrstuvwxyz"
 

ilan

Expert
Licensed User
Longtime User
or just take the set of uppercase letters out of this string:

B4X:
   Dim abc As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

B4X:
   Dim abc As String = "0123456789abcdefghijklmnopqrstuvwxyz"

but like this you could use it not only for username you could use it also for passwords. so better leave it inside and just add .ToLowerCase from the returned string (not inside the function)

like:

B4X:
dim str as string
str = RandomString(12).ToLowerCase
 
Top