網(wǎng)站首頁 編程語言 正文
求0到1之間的隨機數(shù)
使用random模塊中的random()函數(shù),作用就是返回一個[0,1)之間的隨機數(shù)。
import random
print(random.random())
生成0-1之間隨機數(shù) 模擬拋硬幣問題
import random
def count_heads(n):
heads=0
for i in range(n):
if random.random()<=0.5:
heads+=1
return heads
#使用字典記錄100000次實驗每一個隨機變量出現(xiàn)的次數(shù) 重復(fù)10次實驗得到10個隨機變量表示每次實驗生成的10個隨機數(shù)代表正面向上的次數(shù)
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()
'''結(jié)果
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
相關(guān)推薦
- 2023-05-17 Kotlin開發(fā)中open關(guān)鍵字與類名函數(shù)名和變量名的使用方法淺析_Android
- 2022-12-28 詳解Go語言strconv與其他基本數(shù)據(jù)類型轉(zhuǎn)換函數(shù)的使用_Golang
- 2022-03-26 C++成員解除引用運算符的示例詳解_C 語言
- 2022-08-26 C++宏函數(shù)和內(nèi)聯(lián)函數(shù)的使用_C 語言
- 2022-09-25 python學(xué)習(xí)筆記之循環(huán)語句
- 2023-02-05 Python?面向?qū)ο缶幊淘斀鈅python
- 2022-11-01 python?類對象的析構(gòu)釋放代碼演示_python
- 2022-03-07 Android?permission?denied原因歸納和解決辦法_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支