I wasn't sure from the intro page who is maintaining the FAQ, but I
came across a slightly subtle bug in an example:
7.3 How can I choose 5 different numbers between 0 and 51 randomly?
Next method returns an array containing m randomly chosen numbers
between 0 and n.
def sample(n, m)
if m.zero?
[]
else
s = sample(n-1, m-1)
t = rand(n+1)
s.concat s.include?(t) ? n : t
end
end
To write it in non-recursive iterated form,
def sample(n, m)
s = []
((n-m)...n).each do |j|
t = rand(j+2)
s.concat s.include?(t) ? j+1 : t
end
s
end
The code fragments don't compile - concat should be '<<' or push.
Also, I think second sample also does not generate a uniform
distribution of numbers. The first time around the loop, it generates
numbers between 0 and (n-m)+2, the next time from (n-m)+3 and so on. I
_think_ this means that the results will tend to be skewed towards the
low side.
In general, would anyone be interested in Andy and I taking the
English FAQ and updating it? We'd be more than happy to.
Regards
Dave