How to Generate a UUID in Ruby with SecureRandom.uuid
SecureRandom.uuid returns a new random UUID as a string, such as 5b558bcd-a815-462d-afb3-a2616f697fd9. It comes with Ruby, so require 'securerandom' is all you need, and SecureRandom.uuid_v7 makes a time-ordered one.
Syntax
require 'securerandom'
<id> = SecureRandom.uuid
| Part | What it does |
|---|---|
require 'securerandom' | Loads SecureRandom, which is part of Ruby's standard library. |
SecureRandom.uuid | Returns a new random version 4 UUID as a string. |
<id> | The variable that holds it. |
Ruby's documentation for uuid, which SecureRandom provides: "Generate a random v4 UUID (Universally Unique IDentifier)."
The Example
The example makes a UUID and prints it and its length, then makes a second UUID and a version 7 UUID.
Prerequisites
- Ruby. This guide uses Ruby 4.0.10.
Step 1: Write the Code
Create a file named main.rb:

require 'securerandom'
id = SecureRandom.uuid
puts id
puts id.length
puts SecureRandom.uuid
puts SecureRandom.uuid_v7
Step 2: Run It
ruby main.rb
5b558bcd-a815-462d-afb3-a2616f697fd9
36
748001a9-cada-437f-b87b-dd52a6266016
01a22d7c-b273-70ad-8e64-517b22e35717

The command is the same in zsh, bash and PowerShell. Your UUIDs will differ.
What Happened
SecureRandom.uuidreturned a string, ready to print or store, 36 characters long.- The two random UUIDs are unrelated. Both have a
4at the start of their third group, the version digit of a random UUID. uuid_v7has a7there, and starts with the current time, so newer ones sort after older ones.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out require 'securerandom' | uninitialized constant SecureRandom in a plain script | Require it first |
Using rand for IDs that must not be guessed | Ruby's documentation says its algorithm "is not for cryptographical use, you must use SecureRandom for security purpose" | Use SecureRandom |
Expecting uuid to sort by time | Version 4 UUIDs are random | Use uuid_v7 when order matters |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
uninitialized constant SecureRandom (NameError) | securerandom isn't loaded | Add require 'securerandom' |
| Many IDs start with the same characters | They come from uuid_v7, whose first part is the current time | That's how version 7 works; use uuid for fully random IDs |
FAQ
Do I need a gem to make UUIDs in Ruby?
No. SecureRandom is part of Ruby, so a plain require 'securerandom' works without Bundler or a gem.
Is SecureRandom.uuid random enough for IDs that must not be guessed?
Yes. Ruby's documentation describes SecureRandom as "an interface to secure random number generators which are suitable for generating session keys in HTTP cookies, etc." Don't use a UUID as a password or a key, though: anyone who sees it can copy it.
Conclusion
SecureRandom.uuid gives you a random version 4 UUID as a string, and SecureRandom.uuid_v7 a time-ordered one. Require securerandom first.
Additional Resources
- Random::Formatter, whose
uuidanduuid_v7methodsSecureRandomprovides, in the Ruby documentation.
