Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def collision_probability_class_5(x: int, y: int) -> float:
- """
- Positive Example 3:
- H_m = { h : x -> a * x mod 2^k div 2^k-l | a in U and a odd}
- Compute the probability that for a random hash function h from H_m,
- x and y collide under h, that is h(x) = h(y).
- This is equal to the percentage of hash functions h from H_m for
- which h(x) = h(y).
- >>> 2/16
- 0.125
- >>> 1/16
- 0.0625
- >>> import universal_hashing
- >>> universal_hashing.m = 16
- >>> universal_hashing.u = 128
- >>> collision_probability_class_5(1, 16)
- 0.0625
- >>> collision_probability_class_5(3, 24)
- 0.0625
- """
- collision_counter= 0
- number_of_iterations = 0
- l = math.log2(m)
- k = math.log2(u)
- pff =k-l +2
- for a in range (1,u,2): #a muss ungerade sein
- if str(bin(a*x))[pff:k-1] == str(bin(a*y))[pff:k-1]:
- collision_counter += 1
- print("collision")
- number_of_iterations +=1
- else:
- number_of_iterations +=1
- return (collision_counter/number_of_iterations)
- """
- >>> m = 16
- >>> u = 128
- >>> collision_probability_class_5(1, 16)
- Traceback (most recent call last):
- File "<pyshell#86>", line 1, in <module>
- collision_probability_class_5(1, 16)
- File "C:\Users\Hometown Derry\Desktop\universal hashing.py", line 222, in collision_probability_class_5
- if str(bin(a*x))[pff:k-1] == str(bin(a*y))[pff:k-1]:
- TypeError: slice indices must be integers or None or have an __index__ method
- >>> """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement