In [12]:
import numpy as np
import tqdm
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
In [13]:
# flags
people_num = 500
init_coin = 50
iterations = 10000
plot_every = 5000
give_out_coin_each_iteration = 1
get_coin_each_iteration = 1
In [14]:
class Person():
def __init__(self, initmoney : int):
self.coins = init_coin
def give_out_coin(self, coin_num : int, allow_negative=False) -> int:
if allow_negative == False:
if self.coins - coin_num >= 0:
self.coins -= coin_num
return coin_num
else:
return 0
else:
self.coins -= coin_num
return coin_num
def get_coin(self, coin_got : int) -> bool:
self.coins += coin_got
return True
In [15]:
people = [Person(init_coin) for _ in range(people_num)]
coin_distribution_1 = []
# go for test with 1000 iteration
for i in range(iterations):
coin_pool = 0 # init coin_pool
# for every iteration everybody loses 1 coin, unless he doesn't have one
for person in people:
# optional: 20% tax for person's current coin
# coin_pool += person.give_out_coin(person.coins // 5)
coin_pool += person.give_out_coin(give_out_coin_each_iteration)
# now distribute coin from coin_pool to each person
coin_goes = np.random.choice(people_num, coin_pool)
for go in coin_goes:
people[go].get_coin(get_coin_each_iteration)
if i % plot_every == 0:
coin_distribution = [person.coins for person in people]
sns.distplot(coin_distribution, bins=20)
plt.xlabel('coins')
plt.ylabel('distribution')
plt.title('% of people with certain coin number distplot')
plt.show()
people.sort(key=lambda x:x.coins)
In [16]:
coin_dist = [person.coins for person in people]
plt.plot(coin_dist)
Out[16]:
In [17]:
plt.xlabel('person index')
plt.ylabel('his coins')
plt.show()
In [18]:
people_2 = [Person(init_coin) for _ in range(people_num)]
coin_distribution_2 = []
# go for test with 1000 iteration
for i in range(iterations):
coin_pool = 0 # init coin_pool
# for every iteration everybody loses 1 coin, unless he doesn't have one
for person in people_2:
# optional: 20% tax for person's current coin
# coin_pool += person.give_out_coin(person.coins // 5)
coin_pool += person.give_out_coin(give_out_coin_each_iteration, allow_negative=True)
# now distribute coin from coin_pool to each person
coin_goes = np.random.choice(people_num, coin_pool)
for go in coin_goes:
people_2[go].get_coin(get_coin_each_iteration)
if i % plot_every == 0:
coin_distribution_2 = [person.coins for person in people_2]
sns.distplot(coin_distribution_2, bins=20)
plt.xlabel('coins')
plt.ylabel('distribution')
plt.title('% of people with certain coin number distplot')
plt.show()
In [19]:
sns.distplot(coin_distribution)
sns.distplot(coin_distribution_2)
plt.show()
In [ ]: