# How to randomly reorder an array?

**URL:** <https://forum.kirupa.com/t/how-to-randomly-reorder-an-array/222038>\
**Category:** flash\
**Created:** [April 10, 2007, 10:52am UTC](https://forum.kirupa.com/t/how-to-randomly-reorder-an-array/222038 "2007-04-10T10:52:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![schmichael](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@schmichael](https://forum.kirupa.com/u/schmichael)\
**Post date:** [April 10, 2007, 10:52am UTC](https://forum.kirupa.com/t/how-to-randomly-reorder-an-array/222038/1 "2007-04-10T10:52:33Z")

</div>

Hiya, I am sure there is a very simple way to do this but I cant figure it out at all. I am trying to write a function that is passed an array and will then reorder it randomly and return the array. My problem is that as the position is generated randomly I dont know how long the loop must run for, this function doesn’t work but it should give you an idea of what I’m trying to do:  
[AS]  
function randomise(the\_ar:Array):Array {  
var ret\_ar:Array = new Array();  
for (i=0; i\<200; i++) {  
var randomNum:Number = Math.floor(Math.random()_(4-0+1));  
if (ret\_ar[randomNum] == undefined) {  
if (ret\_ar.length\<the\_ar.length) {  
ret\_ar[randomNum] = the\_ar_;  
} else {  
return ret\_ar;  
break  
}  
} else {  
}  
}  
}  
[/AS]  
Thanks for any help 🙂  
Schm
