10 responses to “AS3 Vector shuffle or randomize”

  1. dawe

    Note: We use com.gskinner.utils.Rndm class because it generates random numbers based on a seed number: necessary if you have to reproduce a random output.
    If you don’t have or want to use this class, change line 11
    var s:Number = Rndm.integer(0, vec.length);
    var s:Number = Math.round(Rndm.random()*(inVector.length-1));

    again Rndm!!! And “inVector”

  2. Philip Bulley

    Hey, I came across the same Vector shuffle issue around the time you did too 🙂 Here’s was my solution:


    In think yours will be more efficient as I’m just hijacking the vector sort method.

    By the way, I tried casting a Vector.int to Vector.*, but the vector becomes null within the shuffle method. So did this actually work for you?

  3. Mike

    Great article!
    I noticed there is a slight typo in the last line of this post.
    should be

  4. Joe Billman

    Thanks for the great code snippet! I was looking for a good way to shuffle a Vector.

  5. dSilva

    Thanks for the code. Nice help.
    But seems like there’s still a typo at:

    var s:Number = Math.round(Math.random()*(vec.length));

    Where it should read:

    var s:Number = Math.round(Math.random()*(vec.length-1));
    var s:Number = Math.floor(Math.random()*(vec.length));

  6. Vectorクラスのシャッフル | android application
  7. miniml

    I don’t think
    var s:Number = Math.round(Math.random()*(vec.length));
    will work.
    First of all, since you use round(), getting 0th index is half as likely as getting any other valid index.

    Secondly, it Math.random produces Number close to 1, you will get index out of bounds exception because if you have 5 elements in vector then Math.round(0.999*5) = 5 which will cause to access element with index 5 which is not there.

    Use this one instead:
    var s:Number = Math.floor(Math.random()*(vec.length));

Leave a Reply to miniml Click here to cancel reply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.