首先我下载了python,又在朋友的指导下下载了Anaconda和PyCharm
其中Pycharm非常好用 可以自动把代码改成比较清爽的格式 还可以直接把tab改成缩进四个空格
代码运行结果如下:
然后阅读了指南开始修改代码
开头有两个包用不掉可以去掉
代码中重新定义了一个函数figure1
将注释移到了代码开头
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
import numpy as np
import matplotlib.pyplot as plt
def monitor(Threshold, Max_DonateCoin, Box_sum, Box_per_remain, Max_TakeCoin):
"""算法模拟
Args:
Box_sum : 箱子中剩余硬币数量,初始值
People_Flag : 模拟人们取硬币或放硬币的概率
Threshold : 2.5 阈值,可调: 1~Threshold 为取硬币,Threshold+1 ~10 为放硬币
Max_TakeCoin : 最多可取硬币数量
Max_DonateCoin : 最多可放硬币数量
delata : 取、放硬币数量
Box_per_remain : 每次箱子中硬币余额,list
"""
for x in range(1, 5000): # 循环次数表示参与人数
flag = random.randint(1, 10) # flag 模拟人们取硬币或放硬币的概率
if flag > Threshold:
# 放硬币
delta = random.randint(1, Max_DonateCoin)
delta = random.randint(1, delta) # 模拟了人们捐款可能性,有偏少的倾向
Box_sum = Box_sum + delta
Box_per_remain.append(Box_sum)
else:
# 取硬币
delta = random.randint(1, Max_TakeCoin)
delta = random.randint(delta, Max_TakeCoin) # 模拟了人 取硬币的可能性,偏多的倾向
if Box_sum < delta:
Box_sum = 0 # 如果不够取,则取光
else:
Box_sum = Box_sum - delta
Box_per_remain.append(Box_sum)
print(Box_per_remain)
return Box_per_remain
def figure1(title, xlabel, ylabel, Box_per_remain):
""" 绘图区
"""
fig = plt.figure()
## 1. 标题、X、Y 轴 label
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
x = np.arange(len(Box_per_remain))
## 2. data
plt.plot(x, Box_per_remain, color='r')
plt.bar(x, Box_per_remain, alpha=.5, color='g')
plt.show()
if __name__ == '__main__':
Box_per_remain = monitor(Threshold=2.5, Max_DonateCoin=1, Box_sum=500, Box_per_remain=[500], Max_TakeCoin=5)
title = 'Subway testing'
xlabel = 'Time'
ylabel = 'Money remained'
figure1(title, xlabel, ylabel, Box_per_remain)
仓库地址:https://gitee.com/turan0412/Game/tree/47769898813114cab9bd812e86554702225420ff