Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The Pareto Principle
- # A number of players start with 100 dollars each
- # A coin-toss between randomly chosen players results in payment of 10 from loser to winner
- # Any player whose balance reduces to zero is eliminated and takes no further part
- # Eventually only one player has any money left (has all the money) and is the overall winner
- import random
- num = 20 # Number of players
- leadscore = 0
- ctrtoss = 0
- # Define 1 dummy slot (0) and 'num' slots for players 1 to num containing 100 dollars each
- moneylist = [0] + [100] * num
- while True:
- ctrtoss += 1 # Count number of coin tosses
- # Build a list of active players, being indexes of non-empty slots in moneylist
- activelist = [x for x in range(num+1) if moneylist[x]]
- ctr = len(activelist) # Number of active players
- # Choose Winner randomly
- ra = random.randint(0, ctr-1) # Pick a random index into the active list
- a = activelist[ra] # Get the coin-toss winning Player number
- # Choose loser (b) randomly, but not same as a
- b = a
- while b == a:
- rb = random.randint(0, ctr-1) # Pick a random index into the active list
- b = activelist[rb] # Get the coin-toss losing Player number
- moneylist[b] -= 10 # coin-toss loser loses 10 dollars
- moneylist[a] += 10 # coin-toss winner gains 10 dollars
- if moneylist[b] == 0:
- print("Player", b, "has run out of money and is eliminated")
- if moneylist[a] > leadscore:
- leadscore = moneylist[a]
- print("Player", a, "is leading with", leadscore, "in the bank")
- if leadscore >= num * 100:
- break
- print("The winner is player", a, "after", ctrtoss, "tosses of the coin")
Add Comment
Please, Sign In to add comment