網站首頁 編程語言 正文
該章節我們來學習一下 Python 中非常簡單但也非常有用的模塊 —> random ,此模塊主要用于生成隨機數。接下面我們就來了解一下 random 模塊中最常見的幾種方法。
random.random()
功能:隨即返回 0~1 之間的隨機浮點數(使用非常的簡單,看下方的演示demo)
import random print('第一次', random.random()) print('第二次', random.random()) print('第三次', random.random())
random.uniform()
功能:產生一個 區間 的隨機浮點數(演示 demo 如下:)
import random print('第一次', random.uniform(1, 6)) print('第二次', random.uniform(1, 6)) print('第三次', random.uniform(1, 6))
random.randint()
功能:產生一個 區間 的隨機整數(演示 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()
功能:隨機返回對象中指定數量的元素(演示 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()
功能:獲取區間內的一個隨機數(演示 demo 如下:)
注意:randrange()函數的參數與range()相同,其功能相當于choice(range(start, stop, step)),但并不實際產生range對象,該函數返回值類型是int。
舉例:從給定的范圍中選擇一個偽隨機整數。它可以用一個、兩個或三個參數,來確定一個范圍,就像range函數一樣。例如,randrange(1, 6)從范圍[1,2,3,4,5]中返回某個數字,而randrangre(5,105,5)返回5~100之間的5的倍數(包括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個獎項 ‘謝謝參與’、‘華為手環’ 、‘iphone13’、‘小米電視’、 ‘Mac pro’
2、‘謝謝參與’ 的概率為 50%
3、‘華為手環’ 中獎概率 為 25%
4、‘iphone13’ 中獎概率 為 15%
5、‘小米電視’ 中獎概率 為 8%
6、‘Mac Pro’ 中獎概率 為 2%
import random gifts = ['謝謝參與', '華為手環', '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()
這種方法就是讓我們通過一種概率去抽獎,在我們的實際工作中,經常會有這種類似的小活動讓我們做一些抽獎。這些抽獎的活動就類似于這種方式,只不過有一些算法可能會更精密一些。而 random 模塊可以快速的幫我們隨機一些數字,幫助我們進行一些隨機的處理。
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-26 C++?棧和隊列的實現超詳細解析_C 語言
- 2022-04-20 C++中的函數你真的理解了嗎_C 語言
- 2022-02-24 開機時自動運行批處理
- 2022-07-10 css中border屬性設置
- 2023-08-15 nginx 開啟壓縮
- 2022-09-19 C語言數據結構之單鏈表存儲詳解_C 語言
- 2022-04-02 Docker鏡像發布到Docker?Hub的實現方法_docker
- 2022-08-27 C#使用百度Ueditor富文本框實現上傳文件_C#教程
- 最近更新
-
- 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同步修改后的遠程分支