# # 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. # class Random < RBS::Unnamed::Random_Base # # 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. # def initialize: (?Integer seed) -> void # # Returns a random binary string. The argument `size` specifies the length of # the returned string. # def self.bytes: (Integer size) -> String # # 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 # def ==: (untyped arg0) -> bool # # Returns an arbitrary seed value. This is used by Random.new when no seed value # is specified as an argument. # # Random.new_seed #=> 115032730400174366788466674494640623225 # def self.new_seed: () -> Integer # # Returns a random number using the Ruby system PRNG. # # See also Random#rand. # def self.rand: (?0) -> Float | (Integer | ::Range[Integer] max) -> Integer | (Float | ::Range[Float] max) -> Float | [T < Numeric] (::Range[T]) -> T # # 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] # def self.srand: (?Integer number) -> Integer # # 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" # def self.urandom: (Integer) -> String # # 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 # def seed: () -> Integer private def initialize_copy: (self object) -> self def left: () -> untyped def marshal_dump: () -> untyped def marshal_load: (untyped) -> untyped def state: () -> untyped end class Random::Base < RBS::Unnamed::Random_Base end # # ## Random number formatter. # # Formats generated random numbers in many manners. When `'random/formatter'` is # required, several methods are added to empty core module `Random::Formatter`, # making them available as Random's instance and module methods. # # Standard library SecureRandom is also extended with the module, and the # methods described below are available as a module methods in it. # # ### Examples # # Generate random hexadecimal strings: # # require 'random/formatter' # # prng = Random.new # prng.hex(10) #=> "52750b30ffbc7de3b362" # prng.hex(10) #=> "92b15d6c8dc4beb5f559" # prng.hex(13) #=> "39b290146bea6ce975c37cfc23" # # or just # Random.hex #=> "1aed0c631e41be7f77365415541052ee" # # Generate random base64 strings: # # prng.base64(10) #=> "EcmTPZwWRAozdA==" # prng.base64(10) #=> "KO1nIU+p9DKxGg==" # prng.base64(12) #=> "7kJSM/MzBJI+75j8" # Random.base64(4) #=> "bsQ3fQ==" # # Generate random binary strings: # # prng.random_bytes(10) #=> "\016\t{\370g\310pbr\301" # prng.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337" # Random.random_bytes(6) #=> "\xA1\xE6Lr\xC43" # # Generate alphanumeric strings: # # prng.alphanumeric(10) #=> "S8baxMJnPl" # prng.alphanumeric(10) #=> "aOxAg8BAJe" # Random.alphanumeric #=> "TmP9OsJHJLtaZYhP" # # Generate UUIDs: # # prng.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594" # prng.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab" # Random.uuid #=> "f14e0271-de96-45cc-8911-8910292a42cd" # # All methods are available in the standard library SecureRandom, too: # # SecureRandom.hex #=> "05b45376a30c67238eb93b16499e50cf" # # # Generate a random number in the given range as Random does # # prng.random_number #=> 0.5816771641321361 # prng.random_number(1000) #=> 485 # prng.random_number(1..6) #=> 3 # prng.rand #=> 0.5816771641321361 # prng.rand(1000) #=> 485 # prng.rand(1..6) #=> 3 # module Random::Formatter include RBS::Unnamed::Random_Formatter end