SHARE
TWEET

Array Shuffle (KFA)

ulfben Dec 7th, 2015 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //http://blog.codinghorror.com/the-danger-of-naivete/
  2. //Knuth-Fisher-Yates shuffle
  3. function shuffle(array:Array):Array
  4. {
  5.         var m:int = array.length;
  6.         var t:*;
  7.         var i:int;
  8.        
  9.         while (m){ // While there remain elements to shuffle…
  10.                 i = Math.floor(Math.random() * m--);     // Pick a remaining element…
  11.                 t = array[m]; // And swap it with the current element.
  12.                 array[m] = array[i];
  13.                 array[i] = t;
  14.         }
  15.         return array;
  16. }
RAW Paste Data
Top