作业2

首先我下载了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

转载于:https://www.cnblogs.com/turan0412/p/7566095.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值