Last active
July 7, 2020 06:07
-
-
Save jcayzac/db016c9a872b24df5d0d2eb92169ca68 to your computer and use it in GitHub Desktop.
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
| # $1: items power of 10 (e.g. 3=kilo, 6=mega) | |
| # $2: entropy bits (80=2^80 values) | |
| # Requires `brew install bc` (GNU bc) | |
| birthday_probability() { | |
| function bc() { | |
| declare fmt="$1" | |
| shift | |
| printf 'scale=256\n'"$fmt"'\n' ${1+"$@"} | BC_LINE_LENGTH=0 /usr/local/opt/bc/bin/bc -l | |
| } | |
| declare exact="$(bc '1-e(-((10^%s)^2)/((2^%s)*2))' "$1" "$2")" | |
| declare one_in="$(bc '1/%s' "$exact")" | |
| declare log10="$(bc 'l(%s)/l(10)' "$exact")" | |
| declare exp="$(( ${log10%%.*} - 1))" | |
| declare dec="$(bc '%s/(10^%s)' "$exact" "$exp")" | |
| declare prob="${dec::8}E$exp" | |
| [[ "$prob" != '10.00000E-1' ]] || prob='1' | |
| echo "Collision probability: $prob, or 1 in ${one_in%.*}" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment