Forum: Ruby on Rails Randomizing an Array Help

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Justin (Guest)
on 2006-12-22 20:03
(sorry if I posted this twice, google groups is not working well, I

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) }

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

Thank you!
Hammed M. (Guest)
on 2006-12-23 02:10
(Received via mailing list)
a = [1,2,3,4,5]
a.sort_by {rand}
=> 3,1,5,2,4
This topic is locked and can not be replied to.