Be aware that this destroys the original array by erasing the picked elements. You could clone the array beforehand to avoid this.
(Also note that this is O(length * limit) but can be done in O(limit), but that’s probably no problem for you) Discussion on picking random elements: http://propersubset.com/2010/04/choosing-random-elements.html On Apr 14, 2010, at 16:36, [email protected] wrote: > Thanks Oskar, that does exactly what i wanted > > On Apr 14, 2:31 pm, Oskar Krawczyk <[email protected]> wrote: >> Off the top of my head:http://jsfiddle.net/oskar/rXBcQ/ >> >> ___ >> >> Oskar Krawczykhttp://nouincolor.com >> >> On Wed, Apr 14, 2010 at 2:02 PM, [email protected] < >> >> [email protected]> wrote: >>> Hi guys, >> >>> does any one know if there is a script out there that will return a >>> given number of random elements? >> >>> Mootools has '.getRandom' but this only returns 1 random item from the >>> array. >> >>> I want to be able to specify the amount of random items to return e.g. >> >>> var array = [1,2,3,4,5,6,7,8,9] >> >>> var randoms = array.getRandom(5); //[5,3,7,4,8] >> >>> -- >>> To unsubscribe, reply using "remove me" as the subject.
