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 ...
随机推荐
- zookeeper之 zkServer.sh命令、zkCli.sh命令、四字命令
一.zkServer.sh 1.查看 zkServer.sh 帮助信息[root@bigdata05 bin]# ./zkServer.sh helpZooKeeper JMX enabled by ...
- CentOS7.4安装和配置zabbix4.0
一.安装zabbix前,需要搭建好LAMP环境 二.下载zabbix 进入官网:https://www.zabbix.com/ 更多详细内容请参考官方说明文档,详细的安装要求不贴出来了. https: ...
- Jellyfish详解
一.Jellyfish简介 JELLYFISH是CBCB(Center for Bioinformatics and Computational Biology)的Guillaume Marçais ...
- STL 小白学习(3) vector
#include <iostream> using namespace std; #include <vector> void printVector(vector<in ...
- Vue(六) 表单与 v-model
学习使用 v-model 指令完成表单数据双向绑定 基本用法 表单控件在实际业务较为常见,比如单选,多选.下拉选择.输入框等,用他们可以完成数据的录入.校验.提交等.Vue.js 提供了 v-mode ...
- Vim 安装、配置及复制粘贴操作
1.安装:sudo apt-get install vim 2.配置:cd ~ #进入用户主目录 touch .vimrc #.后缀文件不可见 vi .vimrc #打开文件 输入: set cind ...
- react-router解决锚点跳转问题
添加一个onClick方法.onClick方法传入一个锚点的id,然后用下面的函数来找到锚点并跳转到锚点. scrollToAnchor = (anchorName) => { if (anch ...
- elastic search 常用查询
1.查询mapping curl -X GET "10.0.38.111:1200/metric_data_bus_2018-08-07/_mapping/data_bus?pretty&q ...
- linux 基础测试题
1. 什么是buffer和cache buffer (cpu→内存→磁盘,简称为写缓冲 ):当你网上观看视频且网络波动较大的时候,会发现弹出一个正在缓冲中,这个就是buffer. cache(磁盘→内 ...
- 自动化测试badboy脚本开发(一)
badboy的检查点: 检查点设置例子:以上一节脚本录制方法简单录制搜索“badboy”脚本,在搜索框中选中搜索内容(注意录制脚本后要停止录制)→点击工具栏中的Tools→选择Add Assertio ...