Class: Random
Overview
Random
provides an interface to Ruby's pseudo-random number generator, or PRNG. The PRNG produces a deterministic sequence of bits which approximate true randomness. The sequence may be represented by integers, floats, or binary strings.
The generator may be initialized with either a system-generated or user-supplied seed value by using .srand.
The class method .rand provides the base functionality of Kernel.rand along with better handling of floating point values. These are both interfaces to DEFAULT, the Ruby system PRNG.
.new will create a new PRNG with a state independent of DEFAULT, allowing multiple generators with different seed values or sequence positions to exist simultaneously. Random
objects can be marshaled, allowing sequences to be saved and resumed.
PRNGs are currently implemented as a modified Mersenne Twister with a period of 2**19937-1.
Constant Summary
-
DEFAULT =
Direct access to Ruby's Pseudorandom number generator (PRNG).
rand_default
Class Method Summary
-
.new(seed = Random.new_seed) ⇒ prng
constructor
Creates a new PRNG using #seed to set the initial state.
-
.new_seed ⇒ Integer
Returns an arbitrary seed value.
-
.rand ⇒ Float
Alias of
Random::DEFAULT.rand
. -
.srand(number = Random.new_seed) ⇒ old_seed
Alias for Kernel.srand.
Instance Method Summary
-
#==(prng2) ⇒ Boolean
Returns true if the two generators have the same internal state, otherwise false.
-
#bytes(size) ⇒ String
Returns a random binary string containing
size
bytes. -
#rand ⇒ Float
When
max
is an ::Integer,rand
returns a random integer greater than or equal to zero and less thanmax
. -
#seed ⇒ Integer
Returns the seed value used to initialize the generator.
Constructor Details
.new(seed = Random.new_seed) ⇒ prng
Class Method Details
.new_seed ⇒ Integer
Returns an arbitrary seed value. This is used by .new when no seed value is specified as an argument.
Random.new_seed #=> 115032730400174366788466674494640623225
Alias of Random::DEFAULT.rand
.
.srand(number = Random.new_seed) ⇒ old_seed
Alias for Kernel.srand.
Instance Method Details
#==(prng2) ⇒ Boolean
Returns true if the two generators have the same internal state, otherwise false. Equivalent generators will return the same sequence of pseudo-random numbers. Two generators will generally have the same state only if they were initialized with the same seed
Random.new == Random.new # => false
Random.new(1234) == Random.new(1234) # => true
and have the same invocation history.
prng1 = Random.new(1234)
prng2 = Random.new(1234)
prng1 == prng2 # => true
prng1.rand # => 0.1915194503788923
prng1 == prng2 # => false
prng2.rand # => 0.1915194503788923
prng1 == prng2 # => true
#bytes(size) ⇒ String
Returns a random binary string containing size
bytes.
random_string = Random.new.bytes(10) # => "\xD7:R\xAB?\x83\xCE\xFAkO"
random_string.size # => 10
When max
is an ::Integer, rand
returns a random integer greater than or equal to zero and less than max
. Unlike Kernel.rand, when max
is a negative integer or zero, rand
raises an ::ArgumentError.
prng = Random.new
prng.rand(100) # => 42
When max
is a ::Float, rand
returns a random floating point number between 0.0 and max
, including 0.0 and excluding max
.
prng.rand(1.5) # => 1.4600282860034115
When max
is a ::Range, rand
returns a random number where range.member?(number) == true.
prng.rand(5..9) # => one of [5, 6, 7, 8, 9]
prng.rand(5...9) # => one of [5, 6, 7, 8]
prng.rand(5.0..9.0) # => between 5.0 and 9.0, including 9.0
prng.rand(5.0...9.0) # => between 5.0 and 9.0, excluding 9.0
Both the beginning and ending values of the range must respond to subtract (-
) and add (+
)methods, or rand will raise an ::ArgumentError.
#seed ⇒ Integer
Returns the seed value used to initialize the generator. This may be used to initialize another generator with the same state at a later time, causing it to produce the same sequence of numbers.
prng1 = Random.new(1234)
prng1.seed #=> 1234
prng1.rand(100) #=> 47
prng2 = Random.new(prng1.seed)
prng2.rand(100) #=> 47