# Randomizing / Shuffling an Array

**URL:** <https://forum.kirupa.com/t/randomizing-shuffling-an-array/322052>\
**Category:** open source\
**Created:** [May 9, 2011, 4:28am UTC](https://forum.kirupa.com/t/randomizing-shuffling-an-array/322052 "2011-05-09T04:28:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [May 9, 2011, 4:28am UTC](https://forum.kirupa.com/t/randomizing-shuffling-an-array/322052/1 "2011-05-09T04:28:06Z")

</div>

Hi everyone,  
I was creating something that needed a function for shuffling the contents of an array. In case it is helpful, here it is:

```auto

  public function Main() {
   // constructor code
   
   var tempArray:Array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
   
   trace("original: " + tempArray);
   
   ShuffleArray(tempArray);
   
   trace("new: " + tempArray);
  }
  
  public function ShuffleArray(input:Array)
  {
   for (var i:int = 0; i < input.length; i++)
   {
    var randomIndex:int = Math.floor(Math.random()*input.length);
    var tempValue = input[randomIndex];
    
    input[randomIndex] = input*;
    input* = tempValue;
   }
  }
 }

```

I’ll probably write a tutorial on this later, so feel free to let me know if there is a quicker/faster way of doing this.

:beam:
