# Array shuffle

**URL:** <https://forum.kirupa.com/t/array-shuffle/143053>\
**Category:** Uncategorized\
**Created:** [April 2, 2005, 7:39pm UTC](https://forum.kirupa.com/t/array-shuffle/143053 "2005-04-02T19:39:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stringy](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@stringy](https://forum.kirupa.com/u/stringy)\
**Post date:** [April 2, 2005, 7:39pm UTC](https://forum.kirupa.com/t/array-shuffle/143053/1 "2005-04-02T19:39:07Z")

</div>

```auto
fncShuffle = function () { 
    return Math.floor(Math.random()*3)-1; 

} 

```

I have used functions like this in the past to shuffle arrays and in most files it has worked fine. However occasionally(and only in certain files), i have noticed it returning some negative array indices which has lead to errors when looping through the array in the usual way.  
I know there are other more consistant methods of shuffling but has anyone else noticed this problem or know why it may occur.
