# Randomizing Position in an Array

**URL:** <https://www.ruby-forum.com/t/randomizing-position-in-an-array/80802>\
**Category:** Rails\
**Created:** [December 31, 2006, 12:38am UTC](https://www.ruby-forum.com/t/randomizing-position-in-an-array/80802 "2006-12-31T00:38:29Z")\
**Posts on this page:** 5\
**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 31, 2006, 12:38am UTC](https://www.ruby-forum.com/t/randomizing-position-in-an-array/80802/1 "2006-12-31T00:38:29Z")

</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  
necessary 5 that are in the arr\_set array. 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:** [January 2, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/randomizing-position-in-an-array/80802/2 "2007-01-02T18:21:02Z")

</div>

Please try:  
[http://www.google.com/search?rls=en&q=array+shuffle+ruby](http://www.google.com/search?rls=en&q=array+shuffle+ruby)

-Rob

On Dec 22, 2006, at 4:51 AM, jwcooper wrote:

> It does randomize it, but it only returns 3 values instead of the  
> necessary 5 that are in the arr\_set array. Any ideas why?
> 
> Thank you!

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<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, 6:29pm UTC](https://www.ruby-forum.com/t/randomizing-position-in-an-array/80802/3 "2007-01-02T18:29:04Z")

</div>

Just to save you a little time:

> > myarr = [1,2,3,4,5]  
> > =\> [1, 2, 3, 4, 5]  
> > myarr.sort\_by { rand }  
> > =\> [5, 3, 2, 4, 1]

–Jeremy

On 12/22/06, jwcooper [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It does randomize it, but it only returns 3 values instead of the  
> necessary 5 that are in the arr\_set array. Any ideas why?
> 
> Thank you!
> 
> >

–  
My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<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, 7:36pm UTC](https://www.ruby-forum.com/t/randomizing-position-in-an-array/80802/4 "2007-01-02T19:36:38Z")

</div>

Jeremy McAnally wrote:

> Just to save you a little time:
> 
> > > myarr = [1,2,3,4,5]  
> > > =\> [1, 2, 3, 4, 5]
> > 
> > > myarr.sort\_by { rand }  
> > > =\> [5, 3, 2, 4, 1]
> 
> –Jeremy

This doesn’t seem to belong in a rails discussion, but I was curious and  
looked at performance. It seems the elegant ways are not the best. See  
ruby code below.

Stephan

# Timing different way to shuffle an array in ruby

class Array

# The ruby way of swapping two variable values

def swap!(a,b)  
self[a], self[b] = self[b], self[a]  
end

# the “assembler” way of swapping two variable values

def swap\_with\_int\_var!(a,b)  
c = self[a]  
self[a] = self[b]  
self[b] = c  
end

# Shuffling arrays is usually done by iterating through the indices,

and swapping

# each value with a value at a random position

# shuffling based on push/delete

def shuffle!  
size.downto(1) { |n| push delete\_at(rand(n)) }  
self  
end

# shuffling with the ruby way of swapping

def shuffle\_II  
(size-1).downto(1) { |n| swap!(n, rand(size)) }  
self  
end

# shuffling with the “assembler” way of swapping

def shuffle\_III  
(size-1).downto(1) { |n| swap\_with\_int\_var!(n, rand(size)) }  
self  
end

def shuffle\_IV  
sort\_by { rand }  
end

end

def time\_shuffle\_method(size)  
a = (0…size).to\_a

start\_time = Time.new  
3000.times { yield a }  
end\_time = Time.new

return end\_time - start\_time  
end

# To inspect shuffling methods, invoke with argument “test” and

optionally, size  
if (ARGV[0] == ‘test’)  
size = ARGV[1] ? ARGV[1].to\_i : 5  
puts (0…size).to\_a.shuffle!  
puts ‘–’  
puts (0…size).to\_a.shuffle\_II  
puts ‘–’  
puts (0…size).to\_a.shuffle\_III  
puts ‘–’  
puts (0…size).to\_a.shuffle\_IV

exit  
end

shuffle\_time = time\_shuffle\_method(500) { |a| a.shuffle! }  
puts “Time elapsed for shuffle! : #{shuffle\_time}”

shuffle\_time\_II = time\_shuffle\_method(500) { |a| a.shuffle\_II }  
puts “Time elapsed for shuffle\_II : #{shuffle\_time\_II}”

shuffle\_time\_III = time\_shuffle\_method(500) { |a| a.shuffle\_III }  
puts “Time elapsed for shuffle\_III : #{shuffle\_time\_III}”

shuffle\_time\_IV = time\_shuffle\_method(500) { |a| a.shuffle\_IV }  
puts “Time elapsed for shuffle\_IV : #{shuffle\_time\_IV}”

=begin

Here are results from running on my machine

Conclusion: fancy ruby manipulations take their time  
Stick with the “ordinary” shuffle implementation ( shuffle\_III )

stephan@[/tmp]: ruby shuffle\_timer  
Time elapsed for shuffle! : 4.319548  
Time elapsed for shuffle\_II : 5.956232  
Time elapsed for shuffle\_III : 3.011474  
Time elapsed for shuffle\_IV : 3.672521  
stephan@[/tmp]: ruby shuffle\_timer  
Time elapsed for shuffle! : 4.30994  
Time elapsed for shuffle\_II : 5.902858  
Time elapsed for shuffle\_III : 3.139915  
Time elapsed for shuffle\_IV : 3.692018

Stephan W.  
January 2007

=end

---

<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 3, 2007, 4:31am UTC](https://www.ruby-forum.com/t/randomizing-position-in-an-array/80802/5 "2007-01-03T04:31:05Z")

</div>

Very cool, and very useful information. I appreciate everyones help in  
this!

I did a lot of searching on google, but came up empty, so I am thankful  
for this help.

My situation isn’t extremely time sensitive, but I’m glad to know a  
simple solution such as the 3rd, or 4th option will work well.
