python隨機(jī)數(shù)模塊的相關(guān)代碼示例詳解
python隨機(jī)數(shù)模塊
如果你對(duì)計(jì)算機(jī)語(yǔ)言python很了解的話,那么你對(duì)python中隨機(jī)數(shù)模塊是否了解呢?如果你對(duì)python隨機(jī)數(shù)模塊不是很了解的話,你可以瀏覽以下的文章對(duì)其進(jìn)行了解,希望你會(huì)有所收獲。
隨機(jī)整數(shù):
- >>> import random
- >>> random.randint(0,99)
- 21
隨機(jī)選取0到100間的偶數(shù):
- >>> import random
- >>> random.randrange(0, 101, 2)
- 42
隨機(jī)浮點(diǎn)數(shù):
- >>> import random
- >>> random.random()
- 0.85415370477785668
- >>> random.uniform(1, 10)
- 5.4221167969800881
隨機(jī)字符:
- >>> import random
- >>> random.choice('abcdefg&#%^*f')
- 'd'
多個(gè)字符中選取特定數(shù)量的字符:
- >>> import random
- random.sample('abcdefghij',3)
- ['a', 'd', 'b']
多個(gè)字符中選取特定數(shù)量的字符組成新字符串:
- >>> import random
- >>> import string
- >>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
- eplace(" ","")
- 'fih'
隨機(jī)選取字符串:
- >>> import random
- >>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
- 'lemon'
洗牌:
- >>> import random
- >>> items = [1, 2, 3, 4, 5, 6]
- >>> random.shuffle(items)
- >>> items
- [3, 2, 5, 6, 4, 1]
python隨機(jī)數(shù)模塊的相關(guān)應(yīng)用以及代碼的介紹。
【編輯推薦】
- 用python pylint檢查相關(guān)東西的操作方案詳解
- Python入門的相對(duì)路徑和絕對(duì)路徑詳解
- Python數(shù)據(jù)結(jié)構(gòu)創(chuàng)建的具體應(yīng)用方案詳解
- Python語(yǔ)法的基本概念的闡述
- Python編程語(yǔ)言的實(shí)現(xiàn)內(nèi)幕的相關(guān)介紹