房间里有100个人,每人都有100元钱。他们在玩一个游戏:每轮游戏中,每个人都要拿出一元钱随机给另一个人。最后这100个人的财富分布是怎样的?
用python模拟了一把,之前的思路是先产生数据,再保存成图片序列,最后将图片序列通过转视频(利用OpenCV)或转gif(利用imageio)的方式来展现动态的数据。
这两方法有个缺点:不能立马看可视化的结果,得先生成数据!
所以,有了下面的方法,没用animation,但仍然可以实现python动图,还能绘制bar图哦。
import numpy as np
import matplotlib.pyplot as plt
import os
'''
房间里有100个人,每人都有100元钱。
他们在玩一个游戏:每轮游戏中,每个人都要拿出一元钱随机给另一个人。
最后这100个人的财富分布是怎样的?
'''
def init_wealthy(person_total, init_money):
wealthy = []
for i in range(0, person_total):
wealthy.append(init_money)
return wealthy
def get_id_to(id_from, total_person):
id_to = np.random.randint(0, total_person)
if id_to == id_from: # 假设不能把钱给自己
id_to = get_id_to(id_from, total_person)
# print("%d give id_to %d" % (owner, id_to))
return id_to
def check_dir(dir):
if not os.path.exists(dir):
os.mkdir(dir)
print('mkdir: [%s]' % dir)
def draw_ax(ax, titl