mix up array items.
Monkey Forums/Monkey Beginners/mix up array items.
| ||
hi ! Have you an idea how to mix up array items ? example : MyArray = [1,1,2,2,3,3,4,4,5,5, ...] Give : MyArray = [2,3,5,5,4,2,1,3,1, ...] Thanks. |
| ||
There are snippets in the code archive to shuffe a stack. *Click* Is this what you are looking for? |
| ||
FInally i take two values each at a random array position and swap them inside a for...next loop. |
| ||
That is not the correct way to do it. The way to get a good randomized array is to go through each element, and then pick a random element from the entire array to swap it with (including potentially itself). You can learn about the implementation and variations here: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle In particular you want to check out common errors in the implementation: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Implementation_errors |
| ||
recently saw this really nice visualisation of the Fisher-Yates Shuffle, it may be helpful. I wouldn't ever get worried about the correct way to do anything, however and if you can make your original idea work, then feel free to use it. In a really terrible coding example, I turned an array into a list and then ran a loop x times of simply randomly deciding whether to addfirst or addlast to the list. Compared to the correct way it was terribly inefficient,and possibly caused an imperfect balance of cards (not that anything was noticed by players) but it still didn't cause any slow down on any targets. |
| ||
Oh yep the best implementation is always the one that actually gets used!! Completing your project is always the most important step in having a successful project. |