Advertisement
Guest User

Untitled

a guest
May 26th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. def collision_probability_class_5(x: int, y: int) -> float:
  2. """
  3. Positive Example 3:
  4.  
  5. H_m = { h : x -> a * x mod 2^k div 2^k-l | a in U and a odd}
  6.  
  7. Compute the probability that for a random hash function h from H_m,
  8. x and y collide under h, that is h(x) = h(y).
  9.  
  10. This is equal to the percentage of hash functions h from H_m for
  11. which h(x) = h(y).
  12.  
  13. >>> 2/16
  14. 0.125
  15. >>> 1/16
  16. 0.0625
  17.  
  18. >>> import universal_hashing
  19. >>> universal_hashing.m = 16
  20. >>> universal_hashing.u = 128
  21. >>> collision_probability_class_5(1, 16)
  22. 0.0625
  23. >>> collision_probability_class_5(3, 24)
  24. 0.0625
  25. """
  26.  
  27. collision_counter= 0
  28. number_of_iterations = 0
  29. l = math.log2(m)
  30. k = math.log2(u)
  31. pff =k-l +2
  32.  
  33. for a in range (1,u,2): #a muss ungerade sein
  34. if str(bin(a*x))[pff:k-1] == str(bin(a*y))[pff:k-1]:
  35. collision_counter += 1
  36. print("collision")
  37. number_of_iterations +=1
  38. else:
  39. number_of_iterations +=1
  40.  
  41.  
  42.  
  43. return (collision_counter/number_of_iterations)
  44.  
  45.  
  46.  
  47. """
  48. >>> m = 16
  49. >>> u = 128
  50. >>> collision_probability_class_5(1, 16)
  51. Traceback (most recent call last):
  52. File "<pyshell#86>", line 1, in <module>
  53. collision_probability_class_5(1, 16)
  54. File "C:\Users\Hometown Derry\Desktop\universal hashing.py", line 222, in collision_probability_class_5
  55. if str(bin(a*x))[pff:k-1] == str(bin(a*y))[pff:k-1]:
  56. TypeError: slice indices must be integers or None or have an __index__ method
  57. >>> """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement