# Randomizing Arrays

**URL:** <https://forum.kirupa.com/t/randomizing-arrays/16606>\
**Category:** Uncategorized\
**Created:** [March 26, 2003, 7:40pm UTC](https://forum.kirupa.com/t/randomizing-arrays/16606 "2003-03-26T19:40:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![lostinbeta](https://avatars.discourse-cdn.com/v4/letter/l/db5fbb/32.png) [@lostinbeta](https://forum.kirupa.com/u/lostinbeta)\
**Post date:** [March 26, 2003, 7:40pm UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/1 "2003-03-26T19:40:51Z")

</div>

Hey Ilyas, Sen, Eyez, and all the other super master coders here, I need a little help on something.

Last night I got working on a way to randomize the contents of an array. Well what I need help with is… I was wondering if my method is in-effecient, and if so, how do I make it efficient?

[AS]Array.prototype.randomize = function() {  
var arrayLength = this.length;  
var randNum, tempArray;  
for (i=0; i\<arrayLength; i++) {  
randNum = Math.floor(Math.random()_arrayLength);  
tempArray = this_;  
this\* = this[randNum];  
this[randNum] = tempArray;  
}  
};  
myArray = [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”];  
myArray.randomize();  
trace(myArray);[/AS]

I am not really using it for anything, I just wanted to see if I could find a way to do it because I don’t think there are any AS commands that allow you to do it.

I would appreciate any advice :beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 26, 2003, 7:57pm UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/2 "2003-03-26T19:57:36Z")

</div>

ARGH!!! 😱:hair:

I just checked [http://www.layer51.com/proto/](http://www.layer51.com/proto/) to see if anyone did anything similar for randomizing arrays… AND GUESS WHAT!!!

Thats right, Robert Penner already did one, but oddly enough, it looks EXACTLY like what I did!!!

WTF!?!?! I spent like an hour writing this thing.

[AS]Array.prototype.shuffle = function() {  
var len = this.length;  
for (var i=0; i \< len; i++) {  
var rand = Math.floor(Math.random()_len);  
//swap current index with a random one  
var temp = this_;  
this\* = this[rand];  
this[rand] = temp;  
}  
}[/AS]

Guess I wasted my time ;( :-\ ☹ ☹ :\*(

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 26, 2003, 7:58pm UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/3 "2003-03-26T19:58:52Z")

</div>

Did you learn something? Then you didn’t waste your time!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 26, 2003, 8:00pm UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/4 "2003-03-26T20:00:46Z")

</div>

True… I guess ☹ Still upset though.

Wow, I like ralfs solution (replied to robert penners prototype)…

[AS]function shuffle(a, b) {  
return random(2);  
}  
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];  
a.sort(shuffle);  
trace(a);[/AS]

So simple.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 26, 2003, 9:03pm UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/5 "2003-03-26T21:03:48Z")

</div>

lost, that only means you think like the great coders out there. be proud of yourself.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 26, 2003, 9:32pm UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/6 "2003-03-26T21:32:35Z")

</div>

LOL, never thought about it that way Thor. Hmmm… I may think like them, just at a MUCH MUCH MUCH slower pace 😉

Nah, I am over it now, just kinda got fumed to see that what I did already existed anyway and I coulda saved myself time just by looking first. But you know what, I DID learn something, and I DID accomplish a task I wanted to accomplish on my own, so I don’t have anything to complain about, now I am just hungry.

Ima go find something to eat. Later.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 26, 2003, 11:31pm UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/7 "2003-03-26T23:31:30Z")

</div>

I want to accompish eating this whole orange by myself without using my hands 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 12:05am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/8 "2003-03-27T00:05:37Z")

</div>

…shut up dan.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 12:18am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/9 "2003-03-27T00:18:48Z")

</div>

> \*Originally posted by dan4885 \*  
> \*\*I want to accompish eating this whole orange by myself without using my hands 😛 \*\*

BEST QUOTE EVER!

HOLYL \*\*\*\* i hit 600 posts WTF

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 12:27am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/10 "2003-03-27T00:27:07Z")

</div>

Hey we have like the 3 originals in this thread, remember when we almost made a site between the three of us 😛

Congrats on the 600 posts! Thor, your almost there too!

[size=1]thor’s mom has a huge weiner[/size]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 12:48am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/11 "2003-03-27T00:48:09Z")

</div>

haha, memories…

hey i have an idea! let’s make a site! dan’s dad [size=1]who doesn’t have a member (you know what i’m talking about)[/size] can host us, edwin can draw and design the site, and i can sit back and put my name all over it…oh yeah, and code!!!

_sigh_ those were the days…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 2:46am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/12 "2003-03-27T02:46:34Z")

</div>

i can eat cheetos and smell candles

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 3:24am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/13 "2003-03-27T03:24:12Z")

</div>

I can watch… :sigh:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 4:29am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/14 "2003-03-27T04:29:54Z")

</div>

i have extra cheetos and the candle scent needs more sniffers, how bout it?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 4:38am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/15 "2003-03-27T04:38:55Z")

</div>

I guess that will have to do.

::mmm cheetos::

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [March 27, 2003, 4:47am UTC](https://forum.kirupa.com/t/randomizing-arrays/16606/16 "2003-03-27T04:47:48Z")

</div>

[swf=“[http://www.eploids.com/ChestersSpot/images/chesterSpot.swf](http://www.eploids.com/ChestersSpot/images/chesterSpot.swf) height=416 width=593”][/swf]
