Randomizing Arrays

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:

ARGH!!! :scream: :hair:

I just checked 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 ;( :-\ :frowning: :frowning: :*(

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

True… I guess :frowning: 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.

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

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

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.

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

…shut up dan.

*Originally posted by dan4885 *
**I want to accompish eating this whole orange by myself without using my hands :stuck_out_tongue: **

BEST QUOTE EVER!

HOLYL **** i hit 600 posts WTF

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

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

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

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…

i can eat cheetos and smell candles

I can watch… :sigh:

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

I guess that will have to do.

::mmm cheetos::

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