Skip to content

Instantly share code, notes, and snippets.

@mrkschan
Forked from zadam/collision.py
Last active February 2, 2022 12:38
Show Gist options
  • Save mrkschan/b99946402c00ec36b3d0c3c1035c6aa9 to your computer and use it in GitHub Desktop.
Save mrkschan/b99946402c00ec36b3d0c3c1035c6aa9 to your computer and use it in GitHub Desktop.
Calculate birthday paradox (chance of collision) for very large numbers
#!/usr/bin/python3
# This is used for calculating of birthday paradox for large values
# We're using approximation explained here: http://preshing.com/20110504/hash-collision-probabilities/
# Beware that approximation isn't very precise at smaller params N and K, but gets more precise with large numbers
# see https://docs.python.org/3/library/decimal.html#module-decimal
from decimal import *
# setting decimal precision
getcontext().prec = 30
N = Decimal(input("Hash keyspace size: ")) # number of possible values (number of days in birthday paradox)
K = Decimal(input("Number of inputs: ")) # number of chosen values (number of people in birthday paradox)
exponent = (-K * (K - 1) / (2 * N))
unique_probability = exponent.exp(); # Return the value of the (natural) exponential function e**x at the given number.
collision_probability = 1 - unique_probability
print('Probability of having a collision in {0} hashes in the space of {1} possible hashes is {2}'.format(K, N, collision_probability));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment