Advertisement
Jahus

Rain : Randomly and fairly share among shibes

Oct 4th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import random
  2. import math
  3. from decimal import *
  4.  
  5. # translates x from [a:b] to [alpha:beta]
  6. def translate(x, a, b, alpha, beta):
  7.     y = alpha + ((beta - alpha) * ((x - a)/ (b - a)))
  8.     return y
  9.  
  10. #--------------------------------------------------------------
  11. # Things that should be defined elsewhere
  12. #
  13. tipped = 100 # tipped amount
  14. active = 9 # active users (including the user who tips the soaker)
  15. tip_min = 10 # minimum tip there will be
  16.  
  17. #--------------------------------------------------------------
  18. # Settings for the random soak
  19. #
  20. ceil = 1 # 100%
  21. # floor is defined depending on the tip and will get closer to 100% when the tip gets big
  22. floor = min(translate(Decimal.ln(Decimal(tipped)), Decimal.ln(Decimal(tip_min)), 14, 50, 100), 100)/100
  23.  
  24. print("ceil: %i%% | floor: %i%% | active: %s | tip: %s" % (ceil*100, floor*100, active, tipped))
  25.  
  26. mean = int(tipped / (active - 1)) # the normal tipping amount (same share among all users)
  27. tip_ceil = int(mean*ceil) # 100%
  28. tip_floor = max(int(mean*floor), tip_min) # floor%
  29.  
  30. tips = [] # will be a random array of tip amounts
  31.  
  32. #--------------------------------------------------------------
  33. # The random soak algorithm
  34. #
  35.  
  36. # initializing
  37. remaining = tipped
  38.  
  39. # getting the random tip
  40. for i in range(active - 1):
  41.     _max = min(remaining, tip_ceil)
  42.     _min = min(tip_floor, remaining)
  43.     user_i_tip = int(random.uniform(_min, _max))
  44.     tips.append(user_i_tip)
  45.     remaining = remaining - user_i_tip
  46.  
  47. # sharing the remaining coins
  48. while remaining > tip_min:
  49.     i = int(random.uniform(0, active - 1))
  50.     _tip = int(random.uniform(0, remaining))
  51.     remaining = remaining - _tip
  52.     tips[i] = tips[i] + _tip
  53.  
  54. # tips is now an array of length (active-1) filled with random tip amounts
  55. print(tips)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement