class Array
# Standard in Ruby 1.8.7+. See official documentation[[url]http://ruby-doc.org/core-1.9/classes/Array.html[/url]]
def shuffle
dup.shuffle!
end unless method_defined? :shuffle
# Standard in Ruby 1.8.7+. See official documentation[[url]http://ruby-doc.org/core-1.9/classes/Array.html[/url]]
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!
def sample(n = 1)
n = [n, size].min
tmp = self.clone
n.times do |i|
r = i + Kernel.rand(size - i)
tmp[i], tmp[r] = tmp[r], tmp[i]
end
tmp[(0...n)]
end unless method_defined? :sample
end
class Array
# Standard in Ruby 1.8.7+. See official documentation[[url]http://ruby-doc.org/core-1.9/classes/Array.html[/url]]
def shuffle
dup.shuffle!
end unless method_defined? :shuffle
# Standard in Ruby 1.8.7+. See official documentation[[url]http://ruby-doc.org/core-1.9/classes/Array.html[/url]]
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!
def sample(n = 1)
n = [n, size].min
tmp = self.clone
n.times do |i|
r = i + Kernel.rand(size - i)
tmp[i], tmp[r] = tmp[r], tmp[i]
end
tmp[(0...n)]
end unless method_defined? :sample
end