End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Random::Formatter#random_bytes
random_bytes(n=nil)
lib/random/formatter.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.1.3/lib/random/formatter.rb, line 53
def random_bytes(n=nil)
n = n ? n.to_int : 16
gen_random(n)
end
Random::Formatter#random_bytes generates a random binary string.
The argument n specifies the length of the result string.
If n is not specified or is nil, 16 is assumed. It may be larger in future.
The result may contain any byte: “\x00” - “\xff”.
Example
require 'random/formatter' prng.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6" prng.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"