Forum: Ruby on Rails Randomizing an Array Help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0f6eb0d1e0a8e4d4026d43955f49ed4f?d=identicon&s=25 Justin (Guest)
on 2006-12-22 19:03
(sorry if I posted this twice, google groups is not working well, I
think)

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!
0bcc972918891ef358b0b3dfb4e7f18b?d=identicon&s=25 Hammed Malik (Guest)
on 2006-12-23 01: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.