Shuffle Array
Monkey Forums/Monkey Beginners/Shuffle Array
| ||
Hello everyone! Please, is in Monkey-X any way to do Randomize Array? Thank you in advance! ;-) |
| ||
I use the following - it's in my 'Generic' class so I can shuffle an array of anything (ints, strings, objects or whatever). Usage: Local arr:Int[] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] Generic< Int >.Shuffle( arr ) ---> [ 7 2 0 5 8 6 3 1 4 9 ] Array is fully shuffled Local arr:Int[] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] Generic< Int >.Shuffle( arr, 3 ) ---> [ 6 8 2 3 4 5 0 7 1 9 ] Only the first three elements are random (e.g. if you wanted to get three random cards from a deck, but you don't care if the rest of the deck is shuffled yet. If you don't like generics, it should be easy enough to change it for an array of something particular. Or you could remove the nToShuffle option if you will never want that. And bear in mind that the results will depend on the seed of the random number generator. |
| ||
Yes, it works!! Thank you very much for your help! ;) |