Last active
December 18, 2020 12:50
-
-
Save ngrilly/565bd27f4ad63244f72578844bca5f17 to your computer and use it in GitHub Desktop.
Probability of collision with ULIDs on a 50 million years period
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
ULID probability of collision | |
""" | |
import decimal | |
# A ULID uses 80 bits of entropy | |
d = decimal.Decimal(2**80) | |
# Generate 1,000 ULIDs per millisecond (1,000,000 per second) | |
n = decimal.Decimal(1000) | |
# Probability that all ULIDs are different in 1 millisecond (https://en.wikipedia.org/wiki/Birthday_problem) | |
p = (-n**2 / (2*d)).exp() | |
# Probability of collision in 50 million years | |
millisecond = 1 | |
second = 1000 * millisecond | |
hour = 3600 * second | |
day = 24 * hour | |
year = 365 * day | |
pc = 1 - p**(50*1000*1000*year) | |
# Results | |
print "p =", p | |
print "pc =", pc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can't fathom how good python is for tasks like these