Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. import random
  2. from bisect import bisect_left
  3. from collections import Counter
  4.  
  5. def rand(start, F):
  6.     return start + bisect_left(F, random.random())-1
  7.  
  8. def normalisedCumu(fs):
  9.     tot = sum(fs)
  10.     ret = [0]
  11.     cumu = 0
  12.     for v in fs:
  13.         cumu += v
  14.         ret.append(cumu/tot)
  15.     return ret
  16.  
  17. ### Test ###
  18. #ps = [0, 0.3, 0.4, 1]
  19. start = 70
  20. F = normalisedCumu([1]*10 + [0]*3 + [1]*4)
  21. simuN = 10000
  22. d = Counter([rand(start, F) for _ in range(simuN)])
  23. for k in sorted(d.keys()):
  24.     print (k, ": ", float(d[k])/simuN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement