Python3根据基础概率随机生成选项
想要实现一个功能:不同事件发生的基础概率不同,根据基础概率来随机生成选项。
比如,北京的秋天有四种状态,并分别对应一个基础概率,然后随机生成某一天的天气情况。
weatherlist = ['Sunny', 'Cloudy', 'Windy', 'Rainy']
probaslist = [0.30, 0.35, 0.25, 0.1]
实现脚本如下:
import random
def generate_weather(weather_list, probabilities_list):
if not (0.99999 < sum(probabilities_list) < 1.00001):
raise ValueError("The probabilities are not normalized!")
if len(weather_list) != len(probabilities_list):
raise ValueError("The length of two input lists are not match!")
random_normalized_num = random.random() # random() -> x in the interval [0, 1).
accumulated_probability = 0.0
for item in zip(weather_list, probabilities_list):
accumulated_probability += item[1]
if random_normalized_num < accumulated_probability:
return item[0]
测试运行情况:
weatherlist = ['Sunny', 'Cloudy', 'Windy', 'Rainy']
probaslist = [0.30, 0.35, 0.25, 0.1] output_dict = {}
for x in weatherlist:
output_dict.update({x: 0}) n = 10000
for i in range(n):
weather = generate_weather(weatherlist, probaslist)
output_dict[weather] += 1 percentage_dict = {key: output_dict[key]/n for key in output_dict}
print(output_dict)
print(percentage_dict)
得到的结果为:
{'Sunny': 3033, 'Cloudy': 3466, 'Windy': 2484, 'Rainy': 1017}
{'Sunny': 0.3033, 'Cloudy': 0.3466, 'Windy': 0.2484, 'Rainy': 0.1017}
模拟生成10000次天气情况,将各种天气情况的频率与初始设置的基础概率比较,发现结果很吻合。
巨人的肩膀:
https://www.cnblogs.com/zmlctt/p/4315783.html
Python3根据基础概率随机生成选项的更多相关文章
- Java基础之随机生成数字和字母
字母与数字的ASCII码 目 前计算机中用得最广泛的 字符集及其编码,是由美国国家标准局(ANSI)制定的ASCII码(American Standard Code for Information I ...
- Java基础__随机生成1~15之间不重复的数字
package text; import java.util.ArrayList; import java.util.List; public class Text { public static v ...
- python3 随机生成6位数的验证码
python3 随机生成6位数的验证码 要求是数字:0~9 及大小写字母. #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung ...
- python3 随机生成10以内的加法算术题
今晚晚饭过后,看到小孩在做加法题,全是10以内的,因为她现在只会10以内的加法题.而这些题是老婆手动出的题目. 看到这个情景,突然想到,可以用python来实现随机出题,而且可以指定出多少题,出多少以 ...
- 基于c编写的关于随机生成四则运算的小程序
基于http://www.cnblogs.com/HAOZHE/p/5276763.html改编写的关于随机生成四则运算的小程序 github源码和工程文件地址:https://github.com/ ...
- 转:在0~N(不包括N)范围内随机生成一个长度为M(M <= N)且内容不重复的数组
1. 最朴素暴力的做法. void cal1() { , j = , num = ; int result[M]; result[] = rand() % N; //第一个肯定不重复, 直接加进去 ; ...
- 性能测试--Jmeter随机生成/随机选取/csv读取关键字
Jmeter随机生成/随机选取/csv读取关键字 一.随机生成关键字 随机生成关键字,需要组件:随机变量配置元件(Random Variable) 该组件的作用是生成字符+随机数字格式的字符串,并保 ...
- Djaingo 随机生成验证码(PIL)
基础: https://www.cnblogs.com/wupeiqi/articles/5812291.html 实例: https://www.cnblogs.com/6324TV/p/88112 ...
- [CareerCup] 17.11 Rand7 and Rand5 随机生成数字
17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random numb ...
随机推荐
- A - 不要62 HDU - 2089
#include<stdio.h> #include<string.h> #include<math.h> #include<time.h> #incl ...
- Eslint 从入门到放弃
https://blog.csdn.net/walid1992/article/details/54633760
- 使用Jekins自动构建项目(GitLab+Java Maven)
1. 登录Jekins 前提: Jekins 已经部署完成并能登录版本:Jenkins2.60.2 http://IP:端口/jenkins/view/all/newJob 2. 新建构建任务 选择项 ...
- Thread线程相关方法详解
1.sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁.也就是说如果有synchronized同步快,其他线程仍然不能访问共享数据.注意该方 ...
- Web 安全漏洞之 XSS 攻击
什么是 XSS 攻击 XSS(Cross-Site Scripting)又称跨站脚本,XSS的重点不在于跨站点,而是在于脚本的执行.XSS是一种经常出现在 Web 应用程序中的计算机安全漏洞,是由于 ...
- 读李宏毅《一天看懂深度学习》——Deep Learning Tutorial
大牛推荐的入门用深度学习导论,刚拿到有点懵,第一次接触PPT类型的学习资料,但是耐心看下来收获还是很大的,适合我这种小白入门哈哈. 原PPT链接:http://www.slideshare.net/t ...
- boost高质量随机数库 zhuan
shared_ptr<int> tmp2(new int(10)) ; int * test=tmp2.get(); std::cout<<*test<<" ...
- Win10系列:C#应用控件基础10
PasswordBox控件 在应用程序的登录界面中,有时需要用户输入用户名和密码进行身份验证,应用程序中的密码框可以通过使用PasswordBox控件来实现.PasswordBox控件与TextBox ...
- learning makefile static model
- Python第一发:Hello world
#!/usr/bin/python def main () : print 'hello world' main ()