End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Random::Formatter#hex
hex(n=nil)
lib/random/formatter.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.2.0/lib/random/formatter.rb, line 92
def hex(n=nil)
random_bytes(n).unpack1("H*")
end
Generate a random hexadecimal string.
The argument n specifies the length, in bytes, of the random number to be generated. The length of the resulting hexadecimal string is twice of n.
If n is not specified or is nil, 16 is assumed. It may be larger in the future.
The result may contain 0-9 and a-f.
Example
require 'random/formatter' Random.hex #=> "eb693ec8252cd630102fd0d0fb7c3485" # or prng = Random.new prng.hex #=> "91dc3bfb4de5b11d029d376634589b61"