class RBS::Unnamed::Random_Base
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/random.rbs, line 18
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.
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/random.rbs, line 67
def bytes: (Integer 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
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/random.rbs, line 53
def rand: (?0) -> Float
| (Integer | ::Range[Integer] max) -> Integer
| (Float | ::Range[Float] max) -> Float
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. Note that it behaves differently from Kernel.rand.
prng.rand(1.5) # => 1.4600282860034115 rand(1.5) # => 0
When range 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.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/rbs/unnamed/random.rbs, line 85
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