Android Code Snippet Randomly shuffle a string array

Peter Simpson

Expert
Licensed User
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
What is the purpose of CallSub here ?

ShuffleArray(CardArray) by itself works just as well ?

Regards

Del +++
 

Del

Member
Licensed 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
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.
 

LucaMs

Expert
Licensed User
You are right Luca, but an old IT man like me is not used to this kind of magic! I wrote programs using 80 col punched cards and believe me, there was no F7 around to help me !
Ciao and have a good time...
I'm sorry for you; I'm very young,
upload_2016-7-6_9-9-3.png
as you know (only 55... and 56 on October :()


Ciao, altrettanto a te.
 
Top