Guest User

Untitled

a guest
Nov 19th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. # time needed to generate MicroMint coins.
  2. # balls-and-bins problem
  3. # parameter: u = number of bits used for identifying the bin. b = 2^u
  4. # parameter: k = number of collisions (balls in the same bin) needed to make a coin. Fewer than k balls in the bin makes no coins. k or more balls in a bin makes precisely one coin.
  5. # how many iterations(how many balls thrown) does it take before you generate c = 1, 100, and 10 000 coins respectively?
  6. # all bins are empy at first. At each iteration, throw a ball into a randomly selected bin.
  7. # use t = 0
  8.  
  9. import random
  10. import math
  11.  
  12. lamda = 3.66
  13.  
  14. # input values
  15. u = 20
  16. k = 7
  17. c = 10000
  18. width = 4783
  19. times = 10
  20.  
  21. min_v = 0
  22. max_v = 0
  23.  
  24.  
  25. # balls in bins problem until coins == c
  26. def make_coins(u, k, c):
  27. global min_v
  28. global max_v
  29.  
  30. b = 2 ** u
  31. bins = [0] * b
  32. iterations = 0
  33. coins = 0
  34. while coins < c:
  35. r = random.randint(0, len(bins) - 1)
  36. bins[r] += 1
  37. if bins[r] == k:
  38. coins += 1
  39. iterations += 1
  40.  
  41. if iterations < min_v:
  42. min_v = iterations
  43. elif iterations > max_v:
  44. max_v = iterations
  45. return iterations
  46.  
  47.  
  48. # mean calculation
  49. def mean_value(times):
  50. i_sum = 0
  51. i = 0
  52. for i in range(times):
  53. i_sum += make_coins(u, k, c)
  54.  
  55. return i_sum / times
  56.  
  57.  
  58. mean_v = mean_value(times)
  59. diff = max_v - min_v
  60. deviation = math.sqrt(diff)
  61. check = 3.66 * deviation / math.sqrt(times)
  62.  
  63. if(check < width):
  64. print(mean_v)
  65. else:
  66. print("Outside confidence intervall")
Add Comment
Please, Sign In to add comment