新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
python隨機(jī)數(shù)模塊的相關(guān)代碼示例詳解
python隨機(jī)數(shù)模塊

創(chuàng)新互聯(lián)專業(yè)提供川西大數(shù)據(jù)中心服務(wù),為用戶提供五星數(shù)據(jù)中心、電信、雙線接入解決方案,用戶可自行在線購買川西大數(shù)據(jù)中心服務(wù),并享受7*24小時金牌售后服務(wù)。
如果你對計算機(jī)語言python很了解的話,那么你對python中隨機(jī)數(shù)模塊是否了解呢?如果你對python隨機(jī)數(shù)模塊不是很了解的話,你可以瀏覽以下的文章對其進(jìn)行了解,希望你會有所收獲。
隨機(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'
多個字符中選取特定數(shù)量的字符:
- >>> import random
- random.sample('abcdefghij',3)
- ['a', 'd', 'b']
多個字符中選取特定數(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)用以及代碼的介紹。
標(biāo)題名稱:python隨機(jī)數(shù)模塊的相關(guān)代碼示例詳解
轉(zhuǎn)載源于:http://m.5511xx.com/article/ccoioph.html


咨詢
建站咨詢
