ActionScript 3 - Shuffle (random no repetition)


/ Published in: ActionScript 3
Save to your folder(s)

Random generator with no repetiotion using ActionScript 3


Copy this code and paste it in your HTML
  1. var baseArray:Array;
  2. var numToGet:int = 8;
  3.  
  4. function makeBaseArray(f_length:int):void
  5. {
  6. baseArray = new Array();
  7. for (var i:int = 0; i < f_length; i++)
  8. {
  9. baseArray.push(i);
  10. }
  11. }
  12.  
  13. function getRandomNoRepeat():Number
  14. {
  15. var index:int = Math.random() * baseArray.length;
  16. var value:int = baseArray.splice(index,1)[0];
  17. return value;
  18. }
  19.  
  20. makeBaseArray(16);
  21.  
  22. for (var i:int = 0; i < numToGet; i++)
  23. {
  24. trace(getRandomNoRepeat());
  25. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.