|
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.
|
Greg says: Coding music... Greg says: Hmm... Greg says: I wouldn't rule it out. Chris 3.0 [New features? Yes.] says:
 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:
Yeah, I'll get right on that... right after I learn to fly.
|
|
|
|
nice jobv moose
|
k
|
|
|
|
|
Didn't think you were serious when you said you'd make it.  Nice job.
Last Edit: Aug 2, 2006 15:57:15 GMT by Chris
|
|
|
|
|
|
I feel nostaligic now...PHP...I miss you..;(
|
Ex-admin, designer and founder of Studio Zero. Currently working as a Dentist
|
|
|
|
This would work great for an affiliate thing, nice job Greg.
|
|
|
|
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. Chris 3.0 [New features? Yes.] says:
 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:
Yeah, I'll get right on that... right after I learn to fly.
|
|
|