Lesson 3
UUID v4 and Randomness
How v4 UUIDs use random bits and what collision risk really means.
A UUID v4 contains random bits plus fixed version and variant bits. In practice, that leaves 122 random bits.
The collision risk is extremely low when generated with a good random source. The important condition is quality randomness: use platform cryptography APIs or trusted libraries, not Math.random().
UUID v4 is a good fit for:
- Temporary local records
- Database primary keys where random ordering is acceptable
- Mock data and examples
- Correlation IDs
If you need IDs that sort by creation time, look at UUID v7 or another ordered identifier scheme instead.