Advertisement
joric

probablity.py

May 17th, 2018
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from math import sqrt, log as ln, exp
  3. d, n = 2**32, 1000000
  4. k = sqrt(2*d*ln(2)) + (3-2*ln(2))/6 + (9-4*ln(2)**2) / (72*sqrt(2*d*ln(2))) - 2*(ln(2)**2)/(135*d)
  5. p = 1 - exp(-0.5 * k * (k - 1) / d)
  6. q = n * (n - 1.0) / 2.0 * (1.0 / d)
  7. print('hash bits: %d, samples: %d' % (ln(d)/ln(2), n))
  8. print('precise 0.5 collision probability at %d' % k)
  9. print('checking exponential formula (should be ~ 0.5): %f' % p)
  10. print('square root approximation: %d (%.2f%% precise)' % (sqrt(d), sqrt(d)*100/k) )
  11. print('expected number of collisions: %f' % q)
  12.  
  13. '''
  14. hash bits: 32, samples: 1000000
  15. precise 0.5 collision probability at 77163
  16. checking exponential formula (should be ~ 0.5): 0.499998
  17. square root approximation: 65536 (84.93% precise)
  18. expected number of collisions: 116.415205
  19. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement