How to get a random number in Ruby
如何生成0
到n
之间的随机数字?
Use rand(range)
From Ruby Random Numbers:
If you needed a random integer to simulate a roll of a six-sided die, you'd use: 1 + rand(6)
. A roll in craps could be simulated with 2 + rand(6) + rand(6)
.
Finally, if you just need a random float, just call rand
with no arguments.
As Marc-André Lafortune mentions in his answer below (go upvote it), Ruby 1.9.2 has its own Random
class (that Marc-André himself helped to debug, hence the 1.9.2 target for that feature).
For instance, in this game where you need to guess 10 numbers, you can initialize them with:
10.times.map{ 20 + Random.rand(11) }
#=> [26, 26, 22, 20, 30, 26, 23, 23, 25, 22]
Note:
Using Random.new.rand(20..30)
(using Random.new
) generally would not be a good idea, as explained in detail (again) by Marc-André Lafortune, in his answer (again).
But if you don't use Random.new
, then the class method rand
only takes a max
value, not a Range
, as banister (energetically) points out in the comment (and as documented in the docs for Random
). Only the instance method can take a Range
, as illustrated by generate a random number with 7 digits.
This is why the equivalent of Random.new.rand(20..30)
would be 20 + Random.rand(11)
, since Random.rand(int)
returns “a random integer greater than or equal to zero and less than the argument.” 20..30
includes 30, I need to come up with a random number between 0 and 11, excluding 11.
While you can use rand(42-10) + 10
to get a random number between 10
and 42
(where 10 is inclusive and 42 exclusive), there's a better way since Ruby 1.9.3, where you are able to call:
rand(10...42) # => 13
Available for all versions of Ruby by requiring my backports
gem.
Ruby 1.9.2 also introduced the Random
class so you can create your own random number generator objects and has a nice API:
r = Random.new
r.rand(10...42) # => 22
r.bytes(3) # => "rnd"
The Random
class itself acts as a random generator, so you call directly:
Random.rand(10...42) # => same as rand(10...42)
Notes on Random.new
In most cases, the simplest is to use rand
or Random.rand
. Creating a new random generator each time you want a random number is a really bad idea. If you do this, you will get the random properties of the initial seeding algorithm which are atrocious compared to the properties of the random generator itself.
If you use Random.new
, you should thus call it as rarely as possible, for example once as MyApp::Random = Random.new
and use it everywhere else.
The cases where Random.new
is helpful are the following:
rand
/ Random.rand
that the main programs might be relying on Random
objects can marshalled) If you're not only seeking for a number but also hex or uuid it's worth mentioning that the SecureRandom
module found its way from ActiveSupport
to the ruby core in 1.9.2+. So without the need for a full blown framework:
require 'securerandom'
p SecureRandom.random_number(100) #=> 15
p SecureRandom.random_number(100) #=> 88
p SecureRandom.random_number #=> 0.596506046187744
p SecureRandom.random_number #=> 0.350621695741409
p SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
It's documented here: Ruby 1.9.3 - Module: SecureRandom (lib/securerandom.rb)
链接地址: http://www.djcxy.com/p/17096.html下一篇: 如何在Ruby中获得一个随机数字