Skip to content

Instantly share code, notes, and snippets.

@ngrilly
Last active December 18, 2020 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngrilly/565bd27f4ad63244f72578844bca5f17 to your computer and use it in GitHub Desktop.
Save ngrilly/565bd27f4ad63244f72578844bca5f17 to your computer and use it in GitHub Desktop.
Probability of collision with ULIDs on a 50 million years period
"""
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
@WaiYanMyintMo
Copy link

I can't fathom how good python is for tasks like these

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment