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 ...
随机推荐
- Flex外包公司——案例汇总
Flex做的案例汇总: http://flex.org/showcase/ http://taggraph.com/everybody http://demoprod.informationbuild ...
- ubuntu中可以ping通IP地址但是ping不通域名的问题(www.baidu.com)
治标不治本的办法:每次开机后执行sudo /etc/init.d/resolvconf restart就可以ping通. 治本方法见原博:https://blog.csdn.net/WFping518 ...
- android ------- 开发者的 RxJava 详解
在正文开始之前的最后,放上 GitHub 链接和引入依赖的 gradle 代码: Github: https://github.com/ReactiveX/RxJava https://github. ...
- 基于Python——实现解压文件夹中的.zip文件
[背景]当一个文件夹里存好好多.zip文件需要解压时,手动一个个解压再给文件重命名是一件很麻烦的事情,基于此,今天介绍一种使用python实现批量解压文件夹中的压缩文件并给文件重命名的方法—— [代码 ...
- 『Python CoolBook』数据结构和算法_多变量赋值&“*”的两种用法
多变量赋值 a = [1,2,(3,4)] b,c,d = a print(b,c,d) b,c,(d,e) = a print(b,c,d,e) 1 2 (3, 4) 1 2 3 4 a = &qu ...
- CSS清除浮动的方法及优缺点
浮动是CSS布局里面用的比较多的属性.浮动的框向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 因为浮动会使当前元素产生向上浮的效果,所以浮动的元素会脱离正常的文档流,它包围的内容在 ...
- weex playGround 扫码空白问题
首先安装 weex debug 用 weex debug调试可以看到报错 我做的demo扫码扫不出来 是因为:class的原因 weex中:class只支持数组形式 或者 换成:style就可以 ...
- 【C/C++】Rotate Array
实现数组旋转(循环右移) 如数组 [1, 2, 3, 4, 5, 6, 7],右移 3 位则为 [5, 6, 7, 1, 2, 3, 4] 首先使用泛型函数 void Rotate(void *fro ...
- Vue(八) 数字输入框组件案例
数字输入框是对普通输入框的扩展,用来快捷输入一个标准的数字,如图: 代码: <div id="app"> <input-number v-model=" ...
- spoj Mfish
题解: 先按照pos排序 我们考虑每个船的结束为止endi endi-len[i]+1>=pos[i-1],end[i]>=pos[i],end[i]<pos[i+1] 显然每一个位 ...