Python學習之隨機模塊random詳解
該章節(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ū)間內(nèi)的一個隨機數(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())
以上就是Python學習之隨機模塊random詳解的詳細內(nèi)容,更多關(guān)于Python隨機模塊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一篇文章徹底搞懂Python中可迭代(Iterable)、迭代器(Iterator)與生成器(Generator)的概念
這篇文章主要給大家介紹了如何通過一篇文章徹底搞懂Python中可迭代(Iterable)、迭代器(Iterator)與生成器(Generator)的概念,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-05-05django admin 根據(jù)choice字段選擇的不同來顯示不同的頁面方式
這篇文章主要介紹了django admin 根據(jù)choice字段選擇的不同來顯示不同的頁面方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05