arturh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-24 09:44 UTC
Prototype: shuffle method for Array
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 } --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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, arturh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <arturh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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, arturh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <arturh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
OK, you win :) On 5/24/07, Thomas Fuchs <t.fuchs-moWQItti3gBl57MIdRCFDg@public.gmane.org> 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, arturh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <arturh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Actually I first started using Martin''s algorithm, but felt uneasy because it seems as the outcome depends on the sorting algorithm used. But Thomas'' version doesn''t so I''ll stick with his (and it''s as short as it gets). Thanks a lot I still insist that a shuffle method for Array, (or even Enumerable?) should be part of the Prototype library, isn''t this the place to suggest additions? On May 24, 7:30 pm, Thomas Fuchs <t.fu...-moWQItti3gBl57MIdRCFDg@public.gmane.org> 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, art...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <art...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hey Arturh, arturh a écrit :> I still insist that a shuffle method for Array, (or even Enumerable?) > should be part of the Prototype library, isn''t this the place to > suggest additions?Nope, that would be the Prototype-Core ML. You''re welcome to make your point there, but I should let you know that you may not prevail: shuffling is a pretty marginal need, IMHO. -- Christophe Porteneuve aka TDD tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---