更新:Ruby 1.9.3 Kernel#rand
也接受范围
rand(a..b)
http://www.rubyinside.com/ruby-1-9-3-introduction-and-changes-5428.html
转换为数组可能太贵,而且没有必要.
(a..b).to_a.sample
或
[*a..b].sample
Standard in Ruby 1.8.7+.
Note: was named #choice in 1.8.7 and renamed in later versions.
但无论如何,生成数组需要资源,而您已经编写的解决方案是最好的.