# Add randomize method to array?

**URL:** <https://www.ruby-forum.com/t/add-randomize-method-to-array/158941>\
**Category:** Ruby\
**Created:** [January 29, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/add-randomize-method-to-array/158941 "2009-01-29T16:57:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [January 29, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/add-randomize-method-to-array/158941/1 "2009-01-29T16:57:06Z")

</div>

Perhaps a silly question. What is the best way to add a randomize  
method to array? I know that this code will properly randomize an  
array:

a.sort\_by{ rand }

but I wonder how I can know add this to the array class. I can’t  
assign self to the result of this method, so this will not work:

class Array  
def randomize  
self = self.sort\_by{ rand }  
end  
end

What is the best way to do this?

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [January 29, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/add-randomize-method-to-array/158941/2 "2009-01-29T17:03:41Z")

</div>

On Thu, Jan 29, 2009 at 1:57 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Ruby 1.9 has shuffle and shuffle! methods to shuffle arrays (the ! one  
> being an in-place version).

Ruby 1.8.7 also have shuffle and shuffle!

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [January 29, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/add-randomize-method-to-array/158941/3 "2009-01-29T17:00:06Z")

</div>

On Fri, 30 Jan 2009, Bart B. wrote:

> def randomize  
> self = self.sort\_by{ rand }  
> end  
> end
> 
> What is the best way to do this?

I have no idea how performant this is compared to other ways but the  
first thing that occurs to me is:

class Array  
def randomize  
replace(sort\_by { rand })  
end  
end

Ruby 1.9 has shuffle and shuffle! methods to shuffle arrays (the ! one  
being an in-place version).

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [January 30, 2009, 11:50am UTC](https://www.ruby-forum.com/t/add-randomize-method-to-array/158941/4 "2009-01-30T11:50:14Z")

</div>

2009/1/29 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I consider 1.8.7 to be 1.9.-1 🙂  
> Uh, oh this is spooooky… Reminds me of:

This must thou ken:  
Of one make ten,  
Pass two, and then  
Make square the three,  
So rich thou’lt be.  
Drop out the four!  
From five and six,  
Thus says the witch,  
Make seven and eight.  
So all is straight!  
And nine is one,  
And ten is none,  
This is the witch’s one-time-one!

Original

„Du mußt versteh’n!  
Aus Eins mach Zehn,  
Und Zwei laß geh’n,  
Und Drei mach gleich,  
So bist Du reich.  
Verlier die Vier!  
Aus Fünf und Sechs,  
So sagt die Hex’,  
Mach Sieben und Acht,  
So ist’s vollbracht:  
Und Neun ist Eins,  
Und Zehn ist keins.  
Das ist das Hexen-Einmaleins!"

> **[Hexeneinmaleins](https://de.wikipedia.org/wiki/Hexeneinmaleins)**
>
> Das Hexeneinmaleins (im Original Hexen-Einmal-Eins, in späteren Ausgaben Hexen-Einmaleins) ist in diesem Wortlaut ein Auszug aus Johann Wolfgang von Goethes Faust I in der Szene Hexenküche. Die Szene der Hexenküche und damit auch das Hexeneinmaleins sind erstmals in der Ausgabe Faust, Ein Fragment (gedruckt 1790) vorhanden. Im Urfaust fehlt die Szene noch. Dort ist durch den Handlungsablauf noch keine Verjüngung von Faust erforderlich.
> Mephisto führt Faust in eine Hexenküche, um dort einen Verjü...

Cheers

robert

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [January 29, 2009, 5:04pm UTC](https://www.ruby-forum.com/t/add-randomize-method-to-array/158941/5 "2009-01-29T17:04:52Z")

</div>

On Fri, 30 Jan 2009, Luis P. wrote:

> On Thu, Jan 29, 2009 at 1:57 PM, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Ruby 1.9 has shuffle and shuffle! methods to shuffle arrays (the ! one  
> > being an in-place version).
> 
> Ruby 1.8.7 also have shuffle and shuffle!

I consider 1.8.7 to be 1.9.-1 🙂

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!
