Android Code Snippet ✅ Randomly shuffle a string array

SubName: Shuffle a string array.
Description: Use this code to randomly shuffle a string array.

Example:
"1, 2, 3, 4, 5, 6, 7, 8, 9" will be shuffled to become a random string array, something like "6, 5, 4, 2, 8, 9, 7, 1, 3".
B4X:
Dim CardArray() As String = Array As String(1, 2, 3, 4, 5, 6, 7, 8, 9)

ShuffleArray(CardArray)

'Generate random string array
Public Sub ShuffleArray(StringArray() 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
End Sub

Tags: Shuffle, Random, String, Array
 
Last edited:

Del

Member
Licensed User
Longtime User
What is the purpose of CallSub here ?

ShuffleArray(CardArray) by itself works just as well ?

Regards

Del +++
 

Del

Member
Licensed User
Longtime User
It is not needed.

Many thanks Erel, I came to this page from a discussion here about CallSub and Classes and from that had the impression that there were some finer points that I had not grasped. Simplicity is the key... ?

Thanks for clearing this up for me.

Del +++
 

Peter Simpson

Expert
Licensed User
Longtime User
I only use CallSub in my code as I find using it makes it easier and faster for me to read my code at a glimpse, but that's just me as I speed read at a ridiculously fast rate. I can read a 500+ page book in just a few hours @Del, but CallSub is not actually needed it's just there for my speed reading of my own source code.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…