From: Florian Frank Date: 2006-07-28T04:26:03+09:00 Subject: Re: rand(-10..10) Daniel Martin wrote: > module Enumerable > def rand > ret, i = nil, 0 > each {|v| ret = v if 0 == Kernel.rand(i += 1)} > ret > end > end > > Test code for irb: > > h=Hash.new(0); 10000.times {h[%w{a b c d e}.rand] += 1}; h > I think you shouldn't name this method rand. It shadows Kernel.rand in all classes, that already call rand and include Enumerable. The same is true for Range#rand, if people inherit from Range and already call rand. Ruby makes those things possible, but it's also very easy to shoot yourself or others into the foot, if you aren't careful. The Kernel#rand(-5..15) version wouldn't suffer from this problem. It would be a good idea to add this to ruby core, because this functionality is something, that is needed very often. -- Florian Frank