I needed the ability to randomize the order of an array. This is useful when you have a set collection of questions in a survey that you want to shuffle before you present them to the user. After looking around a bit, I didn't find anything that worked. Here is what I pieced together with my limited knowledge of Ruby.
class Array
def randomize
a=self.dup
result = []
self.length.times do
result << a.slice!(rand(a.length))
end
return result
end
def randomize!
a=self.dup
result = []
self.length.times do
result << a.slice!(rand(a.length))
end
self.replace result
end
end
Here are some test that make sure it really works.
puts "Randomize Tests"
x = [1, 2, 3, 4, 5]
print "x: "
x.each do |i|
print "#{i}"
end
y = x.randomize
print "\ny: "
y.each do |i|
print "#{i}"
end
x.randomize!
print "\nx: "
x.each do |i|
print "#{i}"
end
x.randomize!
print "\nx: "
x.each do |i|
print "#{i}"
end
Special thanks to syntaxhighlighter on providing styles for ruby syntax highlighting.
Update: After doing a search for array shuffle, I found some useful examples.