acclivity

pyParetoPrinciple

Apr 4th, 2021
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. # The Pareto Principle
  2. # A number of players start with 100 dollars each
  3. # A coin-toss between randomly chosen players results in payment of 10 from loser to winner
  4. # Any player whose balance reduces to zero is eliminated and takes no further part
  5. # Eventually only one player has any money left (has all the money) and is the overall winner
  6. import random
  7. num = 20                # Number of players
  8. leadscore = 0
  9. ctrtoss = 0
  10. # Define 1 dummy slot (0) and 'num' slots for players 1 to num containing 100 dollars each
  11. moneylist = [0] + [100] * num
  12. while True:
  13.     ctrtoss += 1        # Count number of coin tosses
  14.     # Build a list of active players, being indexes of non-empty slots in moneylist
  15.     activelist = [x for x in range(num+1) if moneylist[x]]
  16.     ctr = len(activelist)       # Number of active players
  17.     # Choose Winner randomly
  18.     ra = random.randint(0, ctr-1)       # Pick a random index into the active list
  19.     a = activelist[ra]                  # Get the coin-toss winning Player number
  20.     # Choose loser (b) randomly, but not same as a
  21.     b = a
  22.     while b == a:
  23.         rb = random.randint(0, ctr-1)   # Pick a random index into the active list
  24.         b = activelist[rb]              # Get the coin-toss losing Player number
  25.     moneylist[b] -= 10           # coin-toss loser loses 10 dollars
  26.     moneylist[a] += 10           # coin-toss winner gains 10 dollars
  27.     if moneylist[b] == 0:
  28.         print("Player", b, "has run out of money and is eliminated")
  29.     if moneylist[a] > leadscore:
  30.         leadscore = moneylist[a]
  31.         print("Player", a, "is leading with", leadscore, "in the bank")
  32.         if leadscore >= num * 100:
  33.             break
  34. print("The winner is player", a, "after", ctrtoss, "tosses of the coin")
  35.  
Add Comment
Please, Sign In to add comment