Get a random String

Discussion in 'Teaching Programming with B4X' started by ilan, Feb 5, 2017.

  1. ilan

    ilan Expert Licensed User

    you can use this to generate a random userid:

    function:
    Code:
    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:
    Code:
    Log(RandomString(12))
     
    Erel, inakigarm, Widget and 1 other person like this.
  2. Widget

    Widget Well-Known Member Licensed User

    Useful routine. It reminds me of the Youtube url that gets generated to identify each unique video.
    If you use StringBuilder it will run considerably faster. o_O
     
    Erel likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    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
     
    Peter Simpson, ilan and inakigarm like this.
  4. ilan

    ilan Expert Licensed User

    can you explain why? (5ms vs 583ms)

    thank you
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Strings are immutable objects. This means that for each modification a new string is created.
    Consider this code:
    Code:
    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.
    Code:
    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.
     
  6. ilan

    ilan Expert Licensed User

    Thank you @Erel. Very clear answer. :)
     
  7. ilan

    ilan Expert Licensed User

    is this a new tool @Erel? o_O

    no b4x tool has such a line. (Appstart with Args array as string)
     
    Cableguy likes this.
  8. Cableguy

    Cableguy Expert Licensed User

    No-ui b4j??
     
    Peter Simpson, Erel and ilan like this.
  9. ilan

    ilan Expert Licensed User

    thanx @Cableguy, you are right. i just never use b4j non ui app but now i see it can be used as a console app where you can test code sample fast without any UI.
    have never thought about using b4j non ui for that :).
     
  10. Peter Simpson

    Peter Simpson Well-Known Member Licensed User

    We are all always learning @ilan...
     
    ilan and DonManfred like this.
  11. Derek Johnson

    Derek Johnson Active Member Licensed 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.
     
  12. ilan

    ilan Expert Licensed User

    you could use:

    Code:
    Return randomstr.ToLowerCase
    for username ;)
     
  13. Derek Johnson

    Derek Johnson Active Member Licensed User

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

    Code:
    Dim abc As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    Code:
    Dim abc As String = "0123456789abcdefghijklmnopqrstuvwxyz"
     
  14. ilan

    ilan Expert Licensed User

    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:

    Code:
    dim str as string
    str = RandomString(
    12).ToLowerCase
     
Loading...