Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array.shuffle/Array.shuffle!

26 views
Skip to first unread message

Jeff Moore

unread,
Aug 23, 2008, 8:41:16 PM8/23/08
to
This works but I can't escape the nagging sense that there's a more
concise approach (for one thing, it looks like there are way too
transient objects to me).

Anyone have a better idea?


class Array

def shuffle
t_self = self.dup
t_size = self.size
result=[]
t_size.times { result << t_self.slice!(rand(t_self.size)) }
result
end

def shuffle!
t_self = self.dup
t_size = self.size
self.clear
t_size.times { self << t_self.slice!(rand(t_self.size)) }
self
end

end


Regards...
--
Posted via http://www.ruby-forum.com/.

Tim Hunter

unread,
Aug 23, 2008, 8:45:15 PM8/23/08
to
Jeff Moore wrote:
> This works but I can't escape the nagging sense that there's a more
> concise approach (for one thing, it looks like there are way too
> transient objects to me).
>
> Anyone have a better idea?

ary2 = ary.sort_by { rand }

--
RMagick: http://rmagick.rubyforge.org/

Jeff Moore

unread,
Aug 23, 2008, 8:50:40 PM8/23/08
to
Tim Hunter wrote:
> Jeff Moore wrote:
>> This works but I can't escape the nagging sense that there's a more
>> concise approach (for one thing, it looks like there are way too
>> transient objects to me).
>>
>> Anyone have a better idea?
>
> ary2 = ary.sort_by { rand }

"Gone in 60 seconds"...

Thanks

Stefan Rusterholz

unread,
Aug 24, 2008, 6:17:11 AM8/24/08
to

If you need a less predictable one than the sort_by { rand } then I
suggest this:

class Array
# Shuffle the array
def shuffle!
n = length
for i in 0...n
r = Kernel.rand(n-i)+i
self[r], self[i] = self[i], self[r]
end
self
end

# Return a shuffled copy of the array
def shuffle
dup.shuffle!
end
end

This shuffle is known as fisher-yates/knuth shuffle and is afaik by
current means considered impossible to predict. Also its complexity is
O(n) compared to the O(nlogn) of sort_by (which for many cases probably
still is faster in ruby since it runs mainly in C, I didn't bench it,
though).

As of 1.8.7 you have those methods already natively provided by ruby.

Regards
Stefan (apeiros)

Jeff Moore

unread,
Aug 26, 2008, 9:37:45 AM8/26/08
to

> As of 1.8.7 you have those methods already natively provided by ruby.
>
> Regards
> Stefan (apeiros)

Ah! If only the Ubuntu/Debian promotion cycle for Ruby out paced
continental drift...

Thanks again...

I appreciate it.

Joel VanderWerf

unread,
Aug 26, 2008, 2:20:43 PM8/26/08
to
Jeff Moore wrote:
> Ah! If only the Ubuntu/Debian promotion cycle for Ruby out paced
> continental drift...

I heard the 9.04 release will be called Galloping Gondwanaland.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Alexei Broner

unread,
Oct 9, 2008, 3:14:08 PM10/9/08
to
No, 'I' is next in line. "Irresponsible Irmin"

Joel VanderWerf wrote:
> Jeff Moore wrote:
>> Ah! If only the Ubuntu/Debian promotion cycle for Ruby out paced
>> continental drift...
>
> I heard the 9.04 release will be called Galloping Gondwanaland.
--

Posted via http://www.ruby-forum.com/.

0 new messages