Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Random::Formatter#uuid

() -> ::String
uuid

Generate a random v4 UUID (Universally Unique IDentifier).

Example
require 'random/formatter'

Random.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
Random.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
# or
prng = Random.new
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 RFC9562 for details of UUIDv4.

See also #urlsafe_base64 #uuid_v4 #random_number #uuid_v7 also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0