Python學(xué)習(xí)之隨機(jī)模塊random詳解
該章節(jié)我們來(lái)學(xué)習(xí)一下 Python 中非常簡(jiǎn)單但也非常有用的模塊 —> random ,此模塊主要用于生成隨機(jī)數(shù)。接下面我們就來(lái)了解一下 random 模塊中最常見的幾種方法。
random.random()
功能:隨即返回 0~1 之間的隨機(jī)浮點(diǎn)數(shù)(使用非常的簡(jiǎn)單,看下方的演示demo)
import random print('第一次', random.random()) print('第二次', random.random()) print('第三次', random.random())
random.uniform()
功能:產(chǎn)生一個(gè) 區(qū)間 的隨機(jī)浮點(diǎn)數(shù)(演示 demo 如下:)
import random print('第一次', random.uniform(1, 6)) print('第二次', random.uniform(1, 6)) print('第三次', random.uniform(1, 6))
random.randint()
功能:產(chǎn)生一個(gè) 區(qū)間 的隨機(jī)整數(shù)(演示 demo 如下:)
import random print('第一次', random.randint(1, 10)) print('第二次', random.randint(1, 10)) print('第三次', random.randint(1, 10))
random.choice()
功能:返回對(duì)象中的一個(gè)隨機(jī)元素(演示 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()
功能:隨機(jī)返回對(duì)象中指定數(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ū)間內(nèi)的一個(gè)隨機(jī)數(shù)(演示 demo 如下:)
注意:randrange()函數(shù)的參數(shù)與range()相同,其功能相當(dāng)于choice(range(start, stop, step)),但并不實(shí)際產(chǎn)生range對(duì)象,該函數(shù)返回值類型是int。
舉例:從給定的范圍中選擇一個(gè)偽隨機(jī)整數(shù)。它可以用一個(gè)、兩個(gè)或三個(gè)參數(shù),來(lái)確定一個(gè)范圍,就像range函數(shù)一樣。例如,randrange(1, 6)從范圍[1,2,3,4,5]中返回某個(gè)數(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 模塊 - 抽獎(jiǎng)小案例
需求:
1、定義一個(gè)禮品列表,包含5個(gè)獎(jiǎng)項(xiàng) ‘謝謝參與’、‘華為手環(huán)’ 、‘iphone13’、‘小米電視’、 ‘Mac pro’
2、‘謝謝參與’ 的概率為 50%
3、‘華為手環(huán)’ 中獎(jiǎng)概率 為 25%
4、‘iphone13’ 中獎(jiǎng)概率 為 15%
5、‘小米電視’ 中獎(jiǎng)概率 為 8%
6、‘Mac Pro’ 中獎(jiǎng)概率 為 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('中獎(jiǎng)了!你獲得了:', gifts[1]) elif 75 < count <= 90: print('中獎(jiǎng)了!你獲得了:', gifts[2]) elif 90 < count <= 98: print('中獎(jiǎng)了!你獲得了:', gifts[3]) else: print('中獎(jiǎng)了!你獲得了:', gifts[4]) if __name__ == '__main__': chioce_gift()
這種方法就是讓我們通過(guò)一種概率去抽獎(jiǎng),在我們的實(shí)際工作中,經(jīng)常會(huì)有這種類似的小活動(dòng)讓我們做一些抽獎(jiǎng)。這些抽獎(jiǎng)的活動(dòng)就類似于這種方式,只不過(guò)有一些算法可能會(huì)更精密一些。而 random 模塊可以快速的幫我們隨機(jī)一些數(shù)字,幫助我們進(jìn)行一些隨機(jī)的處理。
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('雙色球的中獎(jiǎng)號(hào)碼為:', Lotto())
以上就是Python學(xué)習(xí)之隨機(jī)模塊random詳解的詳細(xì)內(nèi)容,更多關(guān)于Python隨機(jī)模塊的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一篇文章徹底搞懂Python中可迭代(Iterable)、迭代器(Iterator)與生成器(Generator)的概念
這篇文章主要給大家介紹了如何通過(guò)一篇文章徹底搞懂Python中可迭代(Iterable)、迭代器(Iterator)與生成器(Generator)的概念,對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Python測(cè)試框架pytest核心庫(kù)pluggy詳解
這篇文章主要為大家介紹了Python測(cè)試框架pytest核心庫(kù)pluggy使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08django admin 根據(jù)choice字段選擇的不同來(lái)顯示不同的頁(yè)面方式
這篇文章主要介紹了django admin 根據(jù)choice字段選擇的不同來(lái)顯示不同的頁(yè)面方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05在python image 中實(shí)現(xiàn)安裝中文字體
這篇文章主要介紹了在python image 中實(shí)現(xiàn)安裝中文字體,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05wxPython多個(gè)窗口的基本結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了wxPython多個(gè)窗口的基本結(jié)構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11