123456789_123456789_123456789_123456789_123456789_

Class: Random

Relationships & Source Files
Namespace Children
Modules:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Formatter
Inherits: Object
Defined in: random.c,
random.c

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

Class Method Summary

Instance Method Summary

Formatter - Included

Constructor Details

.new(seed = Random.new_seed) ⇒ prng

Creates a new PRNG using #seed to set the initial state. If #seed is omitted, the generator is initialized with .new_seed.

See .srand for more information on the use of seed values.

Class Method Details

.new_seedInteger

Returns an arbitrary seed value. This is used by .new when no seed value is specified as an argument.

Random.new_seed  #=> 115032730400174366788466674494640623225

.randFloat .rand(max) ⇒ Numeric

Alias of Random::DEFAULT.rand.

.raw_seed(size) ⇒ String

Returns a raw seed string, using platform providing features.

Random.raw_seed(8)  #=> "\x78\x41\xBA\xAF\x7D\xEA\xD8\xEA"

.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

#randFloat #rand(max) ⇒ Numeric

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.

#seedInteger

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