OK, you win :)
On 5/24/07, Thomas Fuchs <[EMAIL PROTECTED]> wrote:
>
>
> Array.prototype.shuffle = function(){
> return this.sortBy(Math.random);
> }
>
> Note that the above has a (near impossible) chance to result in an
> endless loop.
>
> Best,
> Thomas
>
> Am 24.05.2007 um 19:09 schrieb Martin Ström:
>
> >
> > Here's a shorter version (wont change the current array but return a
> > new, shuffled, copy:
> >
> > Object.extend(Array.prototype, {
> > shuffle: function() {
> > return this.sort(function() {
> > return Math.floor(Math.random() * 3 -1);
> > });
> > }
> > });
> >
> > /Martin
> >
> >
> > On 5/24/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >>
> >> I needed to shuffle an array, but there was no method so I made one.
> >> Maybe this could be added? I have tested it only on Firefox.
> >> Array.prototype.shuffle = function() {
> >> var j = null // random index from i to last
> >> for (var i=0; i < this.length; ++i) {
> >> j = Math.floor(Math.random()*(this.length-i)) + i
> >> var tmp = this[i]
> >> this[i] = this[j]
> >> this[j] = tmp
> >> }
> >> return this
> >> }
> >>
> >>
> >>>
> >>
> >
> >
> > --
> > burnfield.com/martin
> >
> > >
>
>
> >
>
--
burnfield.com/martin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---