After I published my AS3 Vector shuffle or randomize post, Philip Bulley commented: “…I tried casting a Vector.int to Vector.*, but the vector becomes null within the shuffle method”. I never had to randomize a Vector.<int> but when I tried I discovered Philip was right, the result was null.

I solved this problem by changing my shuffleVector function into a more generic shuffleObject function. It solves all casting problems and you can use the shuffleObject function to randomize Objects, Array and Vectors. So if you want to randomize or shuffle an Object, Array or Vector, please use this function below instead of my previous shuffleVector function.

2 responses to “AS3 Object shuffle or randomize”

Thanks for an interesting blog post. I was just looking into the Fisher-Yates algorithm myself.

I tried running your function multiple times over the array [1,2,3] and it’s displaying a bias towards certain permutations. Here is a sample of the number of each permutation I got over 100000 iterations:

Rather than taking a random value from the length of the list, you take a random value from the iterator which is decremeted each time the while loop is executed. This means that every time you are picking a random item from a smaller and smaller pool of items. I hope that makes sense!