Android Question String no repeat

Tony.

Member
Hi everyone, how can I create a random string ("123456789ABCDEFGHIJKLMNPQRSTUVWXYZ") that does not repeat, to be inserted in my label1?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Dim Characters() As String
End Sub

Sub AppStart (Args() As String)
    Log("Hello world!!!")
    Dim all As String = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ"
    Dim Characters(all.Length) As String
    For i = 0 To all.Length - 1
        Characters(i) = all.CharAt(i)
    Next
    Log(ShuffleArray(Characters, 10))
    Log(ShuffleArray(Characters, 20))
    Log(ShuffleArray(Characters, 3))
End Sub

Public Sub ShuffleArray(StringArray() As String, length As Int) As String
    Dim ArrayVal As String
    Dim Random As Int
 
    For i = 0 To StringArray.Length - 1
        Random = Rnd(i, StringArray.Length)
        ArrayVal = StringArray(i)
        StringArray(i) = StringArray(Random)
        StringArray(Random) = ArrayVal
    Next
    Dim sb As StringBuilder
    sb.Initialize
    For i = 0 To length - 1
        sb.Append(StringArray(i))
    Next
    Return sb.ToString
End Sub

Based on: https://www.b4x.com/android/forum/threads/randomly-shuffle-a-string-array.39435/#content
 
Upvote 0
Top