Skip to content

Instantly share code, notes, and snippets.

@sue445
Last active December 18, 2015 21:09
Show Gist options
  • Save sue445/5844974 to your computer and use it in GitHub Desktop.
Save sue445/5844974 to your computer and use it in GitHub Desktop.
Ruby 1.9と2.0で同じ結果を返す Array#shuffle と同等のメソッド http://sue445.hatenablog.com/entry/2013/06/23/222144
class Array
# ruby 1.9系と2.0系で Array#shuffle の結果が異なるため独自実装
# ref.
# * https://github.com/ruby/ruby/blob/e3efce6df1aa691e17c59f442b35b4fd129d3a13/array.c#L4012
# * https://github.com/ruby/ruby/blob/ruby_2_0_0/NEWS#L35-L36
# * https://github.com/ruby/ruby/commit/e3efce6df1aa691e17c59f442b35b4fd129d3a13
def my_shuffle(random)
i = self.length
dst_array = self.clone
while i > 0 do
j = random.rand(i)
i -= 1
dst_array[i], dst_array[j] = dst_array[j], dst_array[i]
end
dst_array
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment