Suggestion Shuffle/Randomize array items

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
602
To my knowledge there is no inbuilt functionality to get array items in a random order like shuffling a deck of cards and I think this could be useful for all kind of card games.
As a starting point I wrote a function for int arrays using the Fisher–Yates shuffle algorithm, which is quite fast:

Code:
Function RandomizeIntArray:Void(arr:Int[])
    Local x:Int = 0, j:Int
       
    For Local i:Int = arr.Length()-1 To 1  Step -1
        j = Int(Rnd(0, i+1))
        x = arr[j]
        arr[j] = arr[i]
        arr[i] = x
    Next
End


The problem is that it should be easy to implement in the collection classes like Lists or Stacks, but best would be if it was part of the pseudo Methods of array.
Then it needs to be implemented in all of the native languages separately, I guess.
What are your thoughts about this?
 

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
602
Pseudomethods?
This is the term from the docs. I think it means Arrays and Strings are not normal CX classes like Stacks and they are implemented in native code, so their methods just look like cx methods but they are not.
 
Top Bottom