網(wǎng)站首頁 編程語言 正文
該章節(jié)我們來學習一下 Python 中非常簡單但也非常有用的模塊 —> random ,此模塊主要用于生成隨機數(shù)。接下面我們就來了解一下 random 模塊中最常見的幾種方法。
random.random()
功能:隨即返回 0~1 之間的隨機浮點數(shù)(使用非常的簡單,看下方的演示demo)
import random print('第一次', random.random()) print('第二次', random.random()) print('第三次', random.random())
random.uniform()
功能:產(chǎn)生一個 區(qū)間 的隨機浮點數(shù)(演示 demo 如下:)
import random print('第一次', random.uniform(1, 6)) print('第二次', random.uniform(1, 6)) print('第三次', random.uniform(1, 6))
random.randint()
功能:產(chǎn)生一個 區(qū)間 的隨機整數(shù)(演示 demo 如下:)
import random print('第一次', random.randint(1, 10)) print('第二次', random.randint(1, 10)) print('第三次', random.randint(1, 10))
random.choice()
功能:返回對象中的一個隨機元素(演示 demo 如下:)
import random print('第一次', random.choice([1, 'a', 3.14, True, None])) print('第二次', random.choice([1, 'a', 3.14, True, None])) print('第三次', random.choice([1, 'a', 3.14, True, None])) print('第四次', random.choice('abcdefg'))
random.sample()
功能:隨機返回對象中指定數(shù)量的元素(演示 demo 如下:)
import random print('第一次', random.sample(['name', 'age', 'sex', 'height'], 2)) print('第二次', random.sample(['name', 'age', 'sex', 'height'], 2)) print('第三次', random.sample(['name', 'age', 'sex', 'height'], 2)) print('第四次', random.sample('Python', 2))
random.randrange()
功能:獲取區(qū)間內的一個隨機數(shù)(演示 demo 如下:)
注意:randrange()函數(shù)的參數(shù)與range()相同,其功能相當于choice(range(start, stop, step)),但并不實際產(chǎn)生range對象,該函數(shù)返回值類型是int。
舉例:從給定的范圍中選擇一個偽隨機整數(shù)。它可以用一個、兩個或三個參數(shù),來確定一個范圍,就像range函數(shù)一樣。例如,randrange(1, 6)從范圍[1,2,3,4,5]中返回某個數(shù)字,而randrangre(5,105,5)返回5~100之間的5的倍數(shù)(包括5和100,但不包括105。)
import random print(random.randrange(0, 100, 3)) print(random.randrange(5, 55, 5)) print(random.choice(range(6, 48, 3)))
random 模塊 - 抽獎小案例
需求:
1、定義一個禮品列表,包含5個獎項 ‘謝謝參與’、‘華為手環(huán)’ 、‘iphone13’、‘小米電視’、 ‘Mac pro’
2、‘謝謝參與’ 的概率為 50%
3、‘華為手環(huán)’ 中獎概率 為 25%
4、‘iphone13’ 中獎概率 為 15%
5、‘小米電視’ 中獎概率 為 8%
6、‘Mac Pro’ 中獎概率 為 2%
import random gifts = ['謝謝參與', '華為手環(huán)', 'iphone13', '小米電視', 'Mac Pro'] def chioce_gift(): count = random.randrange(0, 100, 1) if 0 <= count <= 50: print(gifts[0]) elif 50 < count <= 75: print('中獎了!你獲得了:', gifts[1]) elif 75 < count <= 90: print('中獎了!你獲得了:', gifts[2]) elif 90 < count <= 98: print('中獎了!你獲得了:', gifts[3]) else: print('中獎了!你獲得了:', gifts[4]) if __name__ == '__main__': chioce_gift()
這種方法就是讓我們通過一種概率去抽獎,在我們的實際工作中,經(jīng)常會有這種類似的小活動讓我們做一些抽獎。這些抽獎的活動就類似于這種方式,只不過有一些算法可能會更精密一些。而 random 模塊可以快速的幫我們隨機一些數(shù)字,幫助我們進行一些隨機的處理。
random 模塊 - 雙色球小案例
# coding:utf-8 import random def Lotto(): red = [] for red_temp in random.sample(range(1, 34), 6): red.append(str(red_temp)) blue = str(random.randint(1, 17)) return ' '.join(red) + ' ' + blue if __name__ == '__main__': print('雙色球的中獎號碼為:', Lotto())
原文鏈接:https://blog.csdn.net/weixin_42250835/article/details/123861379
相關推薦
- 2022-05-19 詳解QTreeWidget隱藏節(jié)點的兩種方式_C 語言
- 2022-09-17 golang構建工具Makefile使用詳解_Golang
- 2022-04-16 C++中不得不說的map容器_C 語言
- 2022-07-11 為Spring配置文件的配置項添加元注釋
- 2022-09-03 golang?四則運算計算器yacc歸約手寫實現(xiàn)_Golang
- 2022-05-29 C++的多態(tài)與虛函數(shù)你了解嗎_C 語言
- 2022-07-17 C語言超詳細講解指針的使用_C 語言
- 2022-09-24 python實現(xiàn)梯度下降求解邏輯回歸_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支