想要实现一个功能:不同事件发生的基础概率不同,根据基础概率来随机生成选项。

比如,北京的秋天有四种状态,并分别对应一个基础概率,然后随机生成某一天的天气情况。

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根据基础概率随机生成选项的更多相关文章

  1. Java基础之随机生成数字和字母

    字母与数字的ASCII码 目 前计算机中用得最广泛的 字符集及其编码,是由美国国家标准局(ANSI)制定的ASCII码(American Standard Code for Information I ...

  2. Java基础__随机生成1~15之间不重复的数字

    package text; import java.util.ArrayList; import java.util.List; public class Text { public static v ...

  3. python3 随机生成6位数的验证码

    python3 随机生成6位数的验证码 要求是数字:0~9 及大小写字母. #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung ...

  4. python3 随机生成10以内的加法算术题

    今晚晚饭过后,看到小孩在做加法题,全是10以内的,因为她现在只会10以内的加法题.而这些题是老婆手动出的题目. 看到这个情景,突然想到,可以用python来实现随机出题,而且可以指定出多少题,出多少以 ...

  5. 基于c编写的关于随机生成四则运算的小程序

    基于http://www.cnblogs.com/HAOZHE/p/5276763.html改编写的关于随机生成四则运算的小程序 github源码和工程文件地址:https://github.com/ ...

  6. 转:在0~N(不包括N)范围内随机生成一个长度为M(M <= N)且内容不重复的数组

    1. 最朴素暴力的做法. void cal1() { , j = , num = ; int result[M]; result[] = rand() % N; //第一个肯定不重复, 直接加进去 ; ...

  7. 性能测试--Jmeter随机生成/随机选取/csv读取关键字

    Jmeter随机生成/随机选取/csv读取关键字 一.随机生成关键字 随机生成关键字,需要组件:随机变量配置元件(Random Variable)  该组件的作用是生成字符+随机数字格式的字符串,并保 ...

  8. Djaingo 随机生成验证码(PIL)

    基础: https://www.cnblogs.com/wupeiqi/articles/5812291.html 实例: https://www.cnblogs.com/6324TV/p/88112 ...

  9. [CareerCup] 17.11 Rand7 and Rand5 随机生成数字

    17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random numb ...

随机推荐

  1. 腾讯云CentOS7.4服务器添加swap分区

    自己的腾讯云服务器搭建的zabbix监控中,提示Lack of free swap space 腾讯的官方说明在这: https://cloud.tencent.com/document/produc ...

  2. Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误

    Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误,一看,还缺 ...

  3. Asp.net core 学习笔记 ( Smtp and Razor template 电子邮件和 Razor 模板 )

    refer : https://dotnetcoretutorials.com/2017/08/20/sending-email-net-core-2-0/ https://ppolyzos.com/ ...

  4. Angular 学习笔记 (Material Select and AutoComplete)

    记入一些思考 : 这 2 个组件有点像,经常会搞混. select 的定位是选择. 目前 select 最糟糕的一点是 not search friendly. 还有当需要 multiple sele ...

  5. 揭示牌面使之升序 Reveal Cards In Increasing Order

    2019-03-27 14:10:37 问题描述: 问题求解: 模拟题.考虑角度是从结果来进行反推. input - [2,3,5,7,11,13,17] (just sort the input t ...

  6. CI集成 mesos 资源分配的思考, 待续

    读了mesos的论文(https://people.eecs.berkeley.edu/~alig/papers/mesos.pdf ),感觉应用在 CI 上的资源管理很赞,能够解决 jenkins在 ...

  7. 由通过seeion识别保存在cookie中的seeionID引发的CSRF问题

    上图是一个完整的CSRF攻击过程解释图 重点是第三句话 用户在没有登出的情况下,被攻击者获得了SESSIONID信息,伪造真用户登录 二.CSRF防御 通过 referer.token 或者 验证码 ...

  8. Win10系列:C#应用控件基础8

    ToggleSwitch控件 在应用程序中ToggleSwitch控件可以模拟一个允许用户在启用和禁用两种状态之间进行切换的物理开关,ToggleSwitch控件的功能与我们在日常生活中所使用的电源开 ...

  9. day10_python_1124

    认知: 随着年龄阅历的变化而变化.01 去年内容回顾 *args **kwargs: 万能参数,动态参数 * 魔性用法: 函数的定义时,* ** 聚合. 函数的执行时,* ** 打散. 形参顺序: 位 ...

  10. yii2 中excel表导出

    首先下载phpexcel 在引入类文件(在web中index.php入口文件或者控制器中引入) require_once dirname(dirname(__FILE__)).'/excel/PHPE ...