# Randomizing an Array?

**URL:** <https://www.ruby-forum.com/t/randomizing-an-array/80248>\
**Category:** Ruby\
**Created:** [December 22, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248 "2006-12-22T20:42:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [December 22, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/1 "2006-12-22T20:42:18Z")

</div>

I’m trying to randomize an array, and this is what I have:

arr\_set\_unordered = randomizer(arr\_set)

def randomizer(arr)  
result = arr.collect { arr.slice!(rand arr.length) }  
end

It does randomize it, but it only returns 3 values instead of the 5  
values that I’m expecting. Any ideas why?

Thank you!

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [December 29, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/2 "2006-12-29T21:15:19Z")

</div>

jwcooper wrote:

> I’m trying to randomize an array…

Try this.

class Array

def randomize  
duplicated\_original, new\_array = self.dup, self.class.new  
new\_array \<\<  
duplicated\_original.slice!(rand(duplicated\_original.size)) until  
new\_array.size.eql?(self.size)  
new\_array  
end

def randomize!  
self.replace(randomize)  
end

end

Enjoy!

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [December 29, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/3 "2006-12-29T21:17:40Z")

</div>

Sorry, I guess a use-case would be helpful.

Call #randomize or #randomize! on any array to mix up its elements.

[5, 10, 15, 20, 25].randomize # [25, 15, 5, 20, 10]

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [January 2, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/4 "2007-01-02T17:36:45Z")

</div>

jwcooper wrote:

> I’m trying to randomize an array, and this is what I have:
> 
> arr\_set\_unordered = randomizer(arr\_set)
> 
> def randomizer(arr)  
> result = arr.collect { arr.slice!(rand arr.length) }  
> end
> 
> It does randomize it, but it only returns 3 values instead of the 5  
> values that I’m expecting. Any ideas why?
> 
> Thank you!

ilans-Mac:~ ilan$ irb  
irb(main):001:0\> (1…10).to\_a.sort {rand}  
=\> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]  
irb(main):002:0\>

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [January 2, 2007, 10:04pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/5 "2007-01-02T22:04:10Z")

</div>

Ilan B. wrote:

> jwcooper wrote:
> 
> > I’m trying to randomize an array, and this is what I have:

[…]

> ilans-Mac:~ ilan$ irb  
> irb(main):001:0\> (1…10).to\_a.sort {rand}  
> =\> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]  
> irb(main):002:0\>

Don’t do this, better use (1…10).sort\_by { rand }. Your version is  
equivalent to (1…10).sort { 1 } and _always_ creates the same  
permutation for this array.

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [January 9, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/6 "2007-01-09T20:30:24Z")

</div>

Florian F. wrote:

> > irb(main):001:0\> (1…10).to\_a.sort {rand}  
> > =\> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]

Wow. That is squeaky clean, and a lot faster than my method. Thanks for  
sharing. 🙂

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [December 30, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/7 "2006-12-30T16:34:03Z")

</div>

I found a library called ‘rand.rb’:  
[http://raa.ruby-lang.org/project/rand/](http://raa.ruby-lang.org/project/rand/). It allows you to select an  
element at random from an array and ‘scramble’ (randomize the order)  
of them as well.

Description:

Helper methods for Enumerable, Array, Hash, and String  
that let you pick a random item or shuffle the order of items.

Examples with Array:

[1,2,3,4].pick

# =\> 2 (pick random element)

[1,2,3,4].shuffle

# =\> [2, 4, 1, 3] (return random-sorted version)

a = [1,2,3,4]  
a.pick!

# =\> 3

a

# =\> [1,2,4] (remove the picked element)

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [January 9, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/8 "2007-01-09T20:34:55Z")

</div>

Florian F. wrote:

> Don’t do this, better use (1…10).sort\_by { rand }. Your version is  
> equivalent to (1…10).sort { 1 } and _always_ creates the same  
> permutation for this array.

It’s random for me. Both work.

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [January 9, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/9 "2007-01-09T23:09:57Z")

</div>

Daniel W. wrote:

> Florian F. wrote:
> 
> > Don’t do this, better use (1…10).sort\_by { rand }. Your version is  
> > equivalent to (1…10).sort { 1 } and _always_ creates the same  
> > permutation for this array.
> 
> It’s random for me. Both work.

No, really, it isn’t. It may “look random”, but try sorting (1…10) many  
times and ponder the coincidence.

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [January 9, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/10 "2007-01-09T22:00:18Z")

</div>

Daniel W. wrote:

> Florian F. wrote:
> 
> > Don’t do this, better use (1…10).sort\_by { rand }. Your version is  
> > equivalent to (1…10).sort { 1 } and _always_ creates the same  
> > permutation for this array.
> 
> It’s random for me. Both work.

Actually Frank is correct, my mistake, it should definetely be sort\_by  
and not sort… I should have reviewed it more thoroughly before  
responding…

ilan

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [May 15, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/11 "2008-05-15T22:52:45Z")

</div>

Florian F. wrote:

> Daniel W. wrote:
> 
> > Florian F. wrote:
> > 
> > > Don’t do this, better use (1…10).sort\_by { rand }. Your version is  
> > > equivalent to (1…10).sort { 1 } and _always_ creates the same  
> > > permutation for this array.
> > 
> > It’s random for me. Both work.
> 
> No, really, it isn’t. It may “look random”, but try sorting (1…10) many  
> times and ponder the coincidence.

That’s because sort{ foo } expects foo to be in -1,0,1.

rand() is always [0, 1).

rand(2)-1 is (-1,1) however and thus will work just fine. 🙂

One could do sort{rand \<=\> rand}, but that’s ~9x slower by my testing.

irb(main):163:0\> (1…10).to\_a.sort{rand }  
=\> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]  
irb(main):164:0\> (1…10).to\_a.sort{rand }  
=\> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]  
irb(main):165:0\> (1…11).to\_a.sort{rand }  
=\> [11, 6, 1, 7, 3, 8, 5, 9, 4, 10, 2]  
irb(main):166:0\> (1…12).to\_a.sort{rand }  
=\> [12, 7, 1, 8, 3, 9, 5, 10, 2, 11, 6, 4]

vs

irb(main):186:0\> (1…12).to\_a.sort{rand(2) -1}  
=\> [1, 2, 6, 11, 9, 4, 8, 7, 5, 10, 3, 12]  
irb(main):187:0\> (1…12).to\_a.sort{rand(2) -1}  
=\> [1, 9, 8, 3, 5, 6, 11, 4, 2, 10, 7, 12]

- Sai [http://saizai.com](http://saizai.com), who is too lazy to remember his actual account  
pass right now

---

<div class="post-metadata">

**Author:** ![jwcooper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f14d63/32.png) [@jwcooper](https://www.ruby-forum.com/u/jwcooper)\
**Post date:** [May 16, 2008, 10:48am UTC](https://www.ruby-forum.com/t/randomizing-an-array/80248/12 "2008-05-16T10:48:44Z")

</div>

2008/5/15 Joao S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > times and ponder the coincidence.
> 
> That’s because sort{ foo } expects foo to be in -1,0,1.

I don’t think so - the sign is important:

irb(main):001:0\> (1…10).sort { -1 }  
=\> [1, 2, 3, 7, 4, 9, 5, 8, 6, 10]  
irb(main):002:0\> (1…10).sort { 1 }  
=\> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]  
irb(main):003:0\> (1…10).sort { 10 }  
=\> [10, 6, 1, 7, 3, 8, 5, 9, 4, 2]  
irb(main):004:0\> (1…10).sort { -10 }  
=\> [1, 2, 3, 7, 4, 9, 5, 8, 6, 10]

> rand() is always [0, 1).

Did you mean [0,1[?

> rand(2)-1 is (-1,1) however and thus will work just fine. 🙂

rand(2)-1 is (-1,0). I would rather do rand(3)-1 or rand(2) - 0.5 -  
But all this is worse than sort\_by { rand } IMHO.

Kind regards

robert
