Skip to content

Instantly share code, notes, and snippets.

@stopthatastronaut
Created March 6, 2018 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stopthatastronaut/80aa5c01a7980de24860045f926af067 to your computer and use it in GitHub Desktop.
Save stopthatastronaut/80aa5c01a7980de24860045f926af067 to your computer and use it in GitHub Desktop.
PowerShell Collision Calculator, aka "The Birthday Problem"
# Birthday Problem aka Birthday Paradox.
# Calculates probability of a collision in a given range of random values
# uses a coarse approximation from
# https://en.wikipedia.org/wiki/Birthday_problem#Approximations
Function Get-CollisionChance
{
param($range, $repetitions)
$d = [double]$range
$n = [double]$repetitions
$a = [math]::pow($n,[double]2)
$b = ([double]2 * $d)
$c = [double]0 - ($a/$b)
$d = [math]::pow([math]::E, $c)
return [double]1 - $d
}
Get-CollisionChance 365 40 # classical birthday problem. 365 days. 40 people. Roughly 88% chance of collision
Get-CollisionChance 365 23 # classical birthday problem. 365 days. 23 people. Generally considered sufficient for a 50/50 chance of collision
Get-CollisionChance 365 10 # classical birthday problem. 365 days. 10 people. Chances of a birthday match around 12%
Get-CollisionChance 365 70 # classical birthday problem. 365 days. 70 people. Should give around a 99.9% chance of a match
Get-CollisionChance 90000 1000 # generalized birthday problem. 90,000 possible values. 1000 variables. Chance of a duplicate > 99.5%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment