網站首頁 編程語言 正文
求0到1之間的隨機數
使用random模塊中的random()函數,作用就是返回一個[0,1)之間的隨機數。
import random
print(random.random())
生成0-1之間隨機數 模擬拋硬幣問題
import random
def count_heads(n):
heads=0
for i in range(n):
if random.random()<=0.5:
heads+=1
return heads
#使用字典記錄100000次實驗每一個隨機變量出現的次數 重復10次實驗得到10個隨機變量表示每次實驗生成的10個隨機數代表正面向上的次數
import collections
d=collections.defaultdict(int)
for i in range(100000):
rv_head=count_heads(10)
d[rv_head]+=1
print(d)
#繪制字典
import matplotlib.pyplot as plt
lists=sorted(d.items())#排序
x,y=zip(*lists)
plt.plot(x,y)
plt.show()
'''結果
defaultdict(<class 'int'>, {4: 20440, 5: 24462, 8: 4305, 6: 20427, 2: 4499, 3: 11905, 7: 11794, 1: 1010, 0: 84, 9: 963, 10: 111})
'''
原文鏈接:https://blog.csdn.net/yugemiren/article/details/115124113
相關推薦
- 2022-08-29 Python?GUI?圖形用戶界面_python
- 2022-08-04 基于Python實現二維圖像雙線性插值_python
- 2022-07-14 C#把dll分別放在指定的文件夾的方法步驟_C#教程
- 2022-07-15 關于在Redis中使用Pipelining加速查詢的問題_Redis
- 2022-11-16 python中內置類型添加屬性問題詳解_python
- 2022-03-17 .NET?6開發TodoList應用實現結構搭建_實用技巧
- 2022-03-29 C#算法之各位相加_C#教程
- 2022-11-03 python數據分析基礎知識之shape()函數的使用教程_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支