M
Max Williams
Here's my final version in case anyone's interested:
class Array
def randomize!(seed=nil)
srand(seed) if seed
i = length - 1
while (i > 0)
#have to specify Kernel::rand to avoid method name clash
j = Kernel::rand(i)
# Swap self and self[j]
tmp = self
self = self[j]
self[j] = tmp
i -= 1
end
#reset srand
srand if seed
self
end
def randomize(seed=nil)
self.dup.randomize!(seed)
end
end
I encountered a puzzling gotcha with rand - it turns out that Rails
monkey-patches Array with 'rand' (no arguments) which pulls out a random
element. So, calling
j = rand(i)
was giving me a 'too many arguments' error, as it thought i was wanting
Array::rand. So, i just specify that i want Kernel::rand and it's fine.
thanks again
max
class Array
def randomize!(seed=nil)
srand(seed) if seed
i = length - 1
while (i > 0)
#have to specify Kernel::rand to avoid method name clash
j = Kernel::rand(i)
# Swap self and self[j]
tmp = self
self = self[j]
self[j] = tmp
i -= 1
end
#reset srand
srand if seed
self
end
def randomize(seed=nil)
self.dup.randomize!(seed)
end
end
I encountered a puzzling gotcha with rand - it turns out that Rails
monkey-patches Array with 'rand' (no arguments) which pulls out a random
element. So, calling
j = rand(i)
was giving me a 'too many arguments' error, as it thought i was wanting
Array::rand. So, i just specify that i want Kernel::rand and it's fine.
thanks again
max