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#uuid
uuid
lib/random/formatter.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.1.3/lib/random/formatter.rb, line 143
def uuid
ary = random_bytes(16).unpack("NnnnnN")
ary[2] = (ary[2] & 0x0fff) | 0x4000
ary[3] = (ary[3] & 0x3fff) | 0x8000
"%08x-%04x-%04x-%04x-%04x%08x" % ary
end
Random::Formatter#uuid generates a random v4 UUID (Universally Unique IDentifier).
Example
require 'random/formatter' prng.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594" prng.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab" prng.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
The version 4 UUID is purely random (except the version). It doesn’t contain meaningful information such as MAC addresses, timestamps, etc.
The result contains 122 random bits (15.25 random bytes).
See RFC 4122 for details of UUID.