Ruby有一个随机数生成器类吗?

Does Ruby Have a Random Number Generator Class?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
How to get a random number in Ruby?

我只是古玩,但是露比是否有一个专门生成随机数的类,比如Java的java. UTIL随机类,或者是露比所有的RAND方法?


内核中有一个rand方法:)

API文档

1
2
3
4
5
6
7
8
rand(max=0) => number
Converts max to an integer using max1 = max.to_i.abs. If the result is zero, returns a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0. Otherwise, returns a pseudorandom integer greater than or equal to zero and less than max1. Kernel::srand may be used to ensure repeatable sequences of random numbers between different runs of the program. Ruby currently uses a modified Mersenne Twister with a period of 2**19937-1.

   srand 1234                 #=> 0
   [ rand,  rand ]            #=> [0.191519450163469, 0.49766366626136]
   [ rand(10), rand(1000) ]   #=> [6, 817]
   srand 1234                 #=> 1234
   [ rand,  rand ]            #=> [0.191519450163469, 0.49766366626136]

有一门"随机"课

请看如何在Ruby中获取随机数的问题。

1
2
10.times.map{ Random.new.rand(20..30) }
#=> [26, 26, 22, 20, 30, 26, 23, 23, 25, 22]