如何在Ruby中随机排序(打乱)数组?

时间:2022-01-31 14:39:27

I'd like to have my array items scrambled. Something like this:

我想把数组项打乱。是这样的:

[1,2,3,4].scramble => [2,1,3,4]
[1,2,3,4].scramble => [3,1,2,4]
[1,2,3,4].scramble => [4,2,3,1]

and so on, randomly

等等,随机

5 个解决方案

#1


255  

Built in now:

建在:

[1,2,3,4].shuffle => [2, 1, 3, 4]
[1,2,3,4].shuffle => [1, 3, 2, 4]

#2


25  

For ruby 1.8.6 (which does not have shuffle built in):

对于ruby 1.8.6(没有内置shuffle):

array.sort_by { rand }

#3


9  

For ruby 1.8.6 as sepp2k's example, but you still want use "shuffle" method.

对于ruby 1.8.6作为sepp2k的例子,但是您仍然需要使用“shuffle”方法。

class Array
  def shuffle
    sort_by { rand }
  end
end

[1,2,3,4].shuffle #=> [2,4,3,1]
[1,2,3,4].shuffle #=> [4,2,1,3]

cheers

干杯

#4


2  

Code from the Backports Gem for just the Array for Ruby 1.8.6. Ruby 1.8.7 or higher is built in.

来自Backports Gem的代码用于Ruby 1.8.6的数组。Ruby 1.8.7或更高版本是内置的。

class Array
  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
  def shuffle
    dup.shuffle!
  end unless method_defined? :shuffle

  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
  def shuffle!
    size.times do |i|
      r = i + Kernel.rand(size - i)
      self[i], self[r] = self[r], self[i]
    end
    self
  end unless method_defined? :shuffle!
end

#5


0  

The Ruby Facets library of extensions has a Random module which provides useful methods including shuffle and shuffle! to a bunch of core classes including Array, Hash and String.

扩展的Ruby facet库有一个随机的模块,它提供了有用的方法,包括洗牌和洗牌!到一堆核心类,包括数组、散列和字符串。

Just be careful if you're using Rails as I experienced some nasty *es in the way its monkeypatching *ed with Rails'...

如果您正在使用Rails,请小心,因为我经历过一些严重的冲突,比如它的monkeypatching与Rails发生冲突……

#1


255  

Built in now:

建在:

[1,2,3,4].shuffle => [2, 1, 3, 4]
[1,2,3,4].shuffle => [1, 3, 2, 4]

#2


25  

For ruby 1.8.6 (which does not have shuffle built in):

对于ruby 1.8.6(没有内置shuffle):

array.sort_by { rand }

#3


9  

For ruby 1.8.6 as sepp2k's example, but you still want use "shuffle" method.

对于ruby 1.8.6作为sepp2k的例子,但是您仍然需要使用“shuffle”方法。

class Array
  def shuffle
    sort_by { rand }
  end
end

[1,2,3,4].shuffle #=> [2,4,3,1]
[1,2,3,4].shuffle #=> [4,2,1,3]

cheers

干杯

#4


2  

Code from the Backports Gem for just the Array for Ruby 1.8.6. Ruby 1.8.7 or higher is built in.

来自Backports Gem的代码用于Ruby 1.8.6的数组。Ruby 1.8.7或更高版本是内置的。

class Array
  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
  def shuffle
    dup.shuffle!
  end unless method_defined? :shuffle

  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
  def shuffle!
    size.times do |i|
      r = i + Kernel.rand(size - i)
      self[i], self[r] = self[r], self[i]
    end
    self
  end unless method_defined? :shuffle!
end

#5


0  

The Ruby Facets library of extensions has a Random module which provides useful methods including shuffle and shuffle! to a bunch of core classes including Array, Hash and String.

扩展的Ruby facet库有一个随机的模块,它提供了有用的方法,包括洗牌和洗牌!到一堆核心类,包括数组、散列和字符串。

Just be careful if you're using Rails as I experienced some nasty *es in the way its monkeypatching *ed with Rails'...

如果您正在使用Rails,请小心,因为我经历过一些严重的冲突,比如它的monkeypatching与Rails发生冲突……