Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does SecureRandom.uuid create a unique string? [closed]

Tags:

ruby

Why does SecureRandom.uuid create a unique string?

SecureRandom.uuid
# => "35cb4e30-54e1-49f9-b5ce-4134799eb2c0"

The string that method SecureRandom.uuid creates is never repeated?

like image 970
super_fish Avatar asked Feb 28 '14 08:02

super_fish


People also ask

Is securerandom UUID unique?

The string is not in fact guaranteed unique. There is a very small but finite chance of a collision. However in practice you will never see two ids generated using this mechanism that are the same, because the probability is so low.

What is SecureRandom in rails?

This library is an interface to secure random number generators which are suitable for generating session keys in HTTP cookies, etc.


1 Answers

The string is not in fact guaranteed unique. There is a very small but finite chance of a collision.

However in practice you will never see two ids generated using this mechanism that are the same, because the probability is so low.

You may safely treat a call to SecureRandom.uuid as generating a globally unique string in code that needs to manage many billions of database entities.

Here is a small table of collision probabilities.

Opinion: If I were to pick an arbitrary limit where you might start to see one or two collisions across the entire dataset with a realistic probability I would go for around 10**16 - assuming you create a million ids per second in your system, then it would take 30 years to reach that size. Even then, the probability of seeing any collisions over the whole 30 years of the project, would be roughly 1 in 100000.

like image 139
Neil Slater Avatar answered Nov 16 '22 03:11

Neil Slater