Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Moose

Moose Avatar

****
Senior Member

449


August 2005
Yay for PHP people! I felt like making a method though more fun than a function. :) This works almost exactly the function for PHP except it's for JavaScript and it's a method.

Array.prototype.array_random = function(){
var devarr = new Array();
var addarr = new Array();
devarr = devarr.concat(this);
if(this.length == 0){
return false;
} else if(this.length == 1){
return devarr;
}
if(arguments[0]){
arguments[0] = parseInt(arguments[0]);
for(a=0;a<arguments[0];a++){
var rannum = Math.floor(Math.random() * devarr.length);
addarr.push(devarr[rannum]);
devarr.splice(rannum,1);
}} else {
var rannum = Math.floor(Math.random() * devarr.length);
addarr.push(devarr[rannum]);
}
return addarr;
}


An example of its use:

var patches = new Array('dog','cat','monster','cowboy');
alert(patches.array_random(2));

That would return three random elements of that array. There is one parameter and that is the number of elements you want to pull randomly from the array. Leaving it blank, will return one element. :o

Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

Andrew McGivery

Andrew McGivery Avatar
Formerly Fredy

******
Legendary Studio Member

Male
5,742


September 2005
nice jobv moose :)
k

Moose

Moose Avatar

****
Senior Member

449


August 2005
Thanks. :)
Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
Didn't think you were serious when you said you'd make it. :P Nice job.


Last Edit: Aug 2, 2006 15:57:15 GMT by Chris

Moose

Moose Avatar

****
Senior Member

449


August 2005
I never lie about these kinds of things(do I?) and thanks. :)
Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

Xylish

Xylish Avatar

******
Ghost Admin

1,895


June 2005
I feel nostaligic now...PHP...I miss you..;(
Ex-admin, designer and founder of Studio Zero. Currently working as a Dentist :)

Nate

Nate Avatar

**
Official Member

85


July 2007
This would work great for an affiliate thing, nice job Greg.

Moose

Moose Avatar

****
Senior Member

449


August 2005
nate said:
This would work great for an affiliate thing, nice job Greg.

Thank you. :)
Greg says:
Coding music...
Greg says:
Hmm...
Greg says:
I wouldn't rule it out. :P
Chris 3.0 [New features? Yes.] says:
:P
Greg says:
If you think about machine code it's a bunch of 1s and 0s
Chris 3.0 [New features? Yes.] says:
Anything is possible.
Greg says:
Yeah try to code Metallica
Chris 3.0 [New features? Yes.] says:
:P Yeah, I'll get right on that... right after I learn to fly.

newBookmarkLockedFalling