class Random
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 Random.srand.
The class method Random.rand provides the base functionality of Kernel.rand along with better handling of floating point values. These are both interfaces to the Ruby system PRNG.
Random.new will create a new PRNG with a state independent of the Ruby system PRNG, 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. As this algorithm is not* for cryptographical use, you must use SecureRandom for security purpose, instead of this PRNG.
See also Random::Formatter module that adds convenience methods to generate various forms of random data.
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 45
def self.bytes: (Integer size) -> String
Returns a random binary string. The argument size specifies the length of the returned string.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 36
def initialize: (?Integer seed) -> void
Creates a new PRNG using seed to set the initial state. If seed is omitted, the generator is initialized with Random.new_seed.
See Random.srand for more information on the use of seed values.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 82
def self.new_seed: () -> Integer
Returns an arbitrary seed value. This is used by Random.new when no seed value is specified as an argument.
Random.new_seed #=> 115032730400174366788466674494640623225
(?0) → Float
(Integer | ::Range[Integer] max) → Integer
(Float | ::Range[Float] max) → Float
[T < Numeric] (::Range[T]) → T
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 94
def self.rand: (?0) -> Float
| (Integer | ::Range[Integer] max) -> Integer
| (Float | ::Range[Float] max) -> Float
| [T < Numeric] (::Range[T]) -> T
Returns a random number using the Ruby system PRNG.
See also Random#rand.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 114
def self.seed: () -> ::Integer
Returns the seed value used to initialize the Ruby system PRNG. 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.
Random.seed #=> 1234 prng1 = Random.new(Random.seed) prng1.seed #=> 1234 prng1.rand(100) #=> 47 Random.seed #=> 1234 Random.rand(100) #=> 47
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 138
def self.srand: (?Integer number) -> Integer
Seeds the system pseudo-random number generator, with number. The previous seed value is returned.
If number is omitted, seeds the generator using a source of entropy provided by the operating system, if available (/dev/urandom on Unix systems or the RSA cryptographic provider on Windows), which is then combined with the time, the process id, and a sequence number.
srand may be used to ensure repeatable sequences of pseudo-random numbers between different runs of the program. By setting the seed to a known value, programs can be made deterministic during testing.
srand 1234 # => 268519324636777531569100071560086917274 [ rand, rand ] # => [0.1915194503788923, 0.6221087710398319] [ rand(10), rand(1000) ] # => [4, 664] srand 1234 # => 1234 [ rand, rand ] # => [0.1915194503788923, 0.6221087710398319]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 155
def self.urandom: (Integer) -> String
Returns a string, using platform providing features. Returned value is expected to be a cryptographically secure pseudo-random number in binary form. This method raises a RuntimeError if the feature provided by platform failed to prepare the result.
In 2017, Linux manpage random(7) writes that “no cryptographic primitive available today can hope to promise more than 256 bits of security”. So it might be questionable to pass size > 32 to this method.
Random.urandom(8) #=> "\x78\x41\xBA\xAF\x7D\xEA\xD8\xEA"
Public Instance Methods
(untyped arg0) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 71
def ==: (untyped arg0) -> bool
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
(self object) → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 176
def initialize_copy: (self object) -> self
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 178
def left: () -> untyped
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 180
def marshal_dump: () -> untyped
(untyped) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 182
def marshal_load: (untyped) -> untyped
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 172
def 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
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/random.rbs, line 184
def state: () -> untyped