python中的隨機函數(shù)小結(jié)
本系列不會對python語法,理論作詳細說明;所以不是一個學習教材;而這里只是我一個學習python的某些專題的總結(jié)。
1. random()函數(shù)
描述:random() 方法返回隨機生成的一個實數(shù),它在[0,1)范圍內(nèi)。
語法:
import random random.random();
注意:random()是不能直接訪問的,需要導入 random 模塊,然后通過 random 靜態(tài)對象調(diào)用該方法。
實例演示:
>>> import random >>> print random.random(); 0.803119901575 >>> print random.random(); 0.451592468747
2. randrange()函數(shù)
描述: randrange() 方法返回指定遞增基數(shù)集合中的一個隨機數(shù),基數(shù)缺省值為1。返回一個整數(shù)
語法
import random random.randrange ([start,] stop [,step])
參數(shù):
- start -- 指定范圍內(nèi)的開始值,包含在范圍內(nèi)
- stop -- 指定范圍內(nèi)的結(jié)束值,不包含在范圍內(nèi)。
- step -- 指定遞增基數(shù)
實例演示
>>> print random.randrange(10); 4 >>> print random.randrange(5,10); 7 >>> print random.randrange(5,10,3); 5 >>> print random.randrange(5,10,3); 8
3.randint()函數(shù)
描述:randint()方法將隨機生成一個整數(shù),它在[x,y]范圍內(nèi) ;有點等價于randrange(x,y+1).
語法
import random random.randint(x,y)
參數(shù):
- x -- 指定范圍內(nèi)的開始值,包含在范圍內(nèi)
- y -- 指定范圍內(nèi)的結(jié)束值,包含在范圍內(nèi)。
實例演示
>>> print random.randrange(5,10); 9 >>> print random.randint(5,10); 6
4. uniform()函數(shù)
描述:uniform() 方法將隨機生成下一個實數(shù),它在[x,y]范圍內(nèi)。返回一個浮點數(shù)
語法:
import random random.uniform (x,y)
參數(shù):
- x -- 指定范圍內(nèi)的開始值,包含在范圍內(nèi)
- y -- 指定范圍內(nèi)的結(jié)束值,包含在范圍內(nèi)。
實例演示
>>> print random.uniform(5,10); 9.13282585434 >>> print random.uniform(9,10); 9.95958315062
5. choice()函數(shù)
描述:choice() 方法返回一個列表,元組或字符串的隨機項。
語法
import random random.choice(x)
參數(shù):
x -- list,tuple,strings的一種
實例演示
>>> print random.choice(('a','be',5,'e'))
5
>>> print random.choice([10,2,6,5,85,'af'])
85
>>> print random.choice('i love python')
v
6. sample()函數(shù)
描述:sample()方法返回隨機從列表,元組或字符串其中部分隨機項 ;返回類型為元組類型
語法
import random random.sample(x,n)
參數(shù):
- x -- list,tuple,strings的一種
- n -- 返回n個隨機項
實例演示
>>> print random.sample('i love python',3)
[' ', 'e', 'i']
>>> print random.sample([10,20,50,23,'ab'],3)
[50, 'ab', 23]
>>> print random.sample((10,20,50,23,'ab'),3)
[50, 20, 'ab']
7. shuffle()函數(shù)
描述:shuffle() 方法將序列的所有元素隨機排序。類似于洗牌
語法 :
import random random.shuffle(x)
參數(shù):
- x -- list,tuple的一種;python2.x只支持list類型
實例演示
>>> list=['a','b','c','d','e']; >>> random.shuffle(list); >>> print list; ['c', 'd', 'a', 'e', 'b']
拓展:將元祖反轉(zhuǎn);實現(xiàn)reverse函數(shù)的效果
>>> list=['a','b','c','d','e']; >>> list1=list[::-1] >>> print list1 ['e', 'd', 'c', 'b', 'a']
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Python內(nèi)置函數(shù)詳談
- python基礎(chǔ)之內(nèi)置函數(shù)
- python高級內(nèi)置函數(shù)用法實例
- Python初學者必須掌握的25個內(nèi)置函數(shù)詳解
- 10個使用Python必須知道的內(nèi)置函數(shù)
- Python函數(shù)的作用域及內(nèi)置函數(shù)詳解
- Python隨機函數(shù)random隨機獲取數(shù)字、字符串、列表等使用詳解
- Python隨機函數(shù)庫random的使用方法詳解
- Python隨機函數(shù)random()使用方法小結(jié)
- Python 內(nèi)置函數(shù)之隨機函數(shù)詳情
相關(guān)文章
Flask與FastAPI對比選擇最佳Python?Web框架的超詳細指南
Flask和FastAPI都是流行的Python?Web框架,各有特點,Flask輕量級、靈活,適合小型項目和原型開發(fā)但不支持異步操作,FastAPI高性能、支持異步,內(nèi)置數(shù)據(jù)驗證和自動生成API文檔,適合高并發(fā)和API開發(fā),需要的朋友可以參考下2025-02-02
pycharm使用conda創(chuàng)建的虛擬環(huán)境時找不到python.exe解決辦法
這篇文章主要給大家介紹了關(guān)于pycharm使用conda創(chuàng)建的虛擬環(huán)境時找不到python.exe的解決辦法,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考價值,需要的朋友可以參考下2024-03-03
python使用socket進行簡單網(wǎng)絡(luò)連接的方法
這篇文章主要介紹了python使用socket進行簡單網(wǎng)絡(luò)連接的方法,實例分析了Python使用socket的基本技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
Python實現(xiàn)把數(shù)字轉(zhuǎn)換成中文
這篇文章主要介紹了Python實現(xiàn)把數(shù)字轉(zhuǎn)換成中文,一般用于數(shù)字金額轉(zhuǎn)中文大寫金額,即將阿拉伯數(shù)字轉(zhuǎn)換為大寫的中文,需要的朋友可以參考下2015-06-06
aws 通過boto3 python腳本打pach的實現(xiàn)方法
這篇文章主要介紹了aws 通過boto3 python腳本打pach的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Python基于opencv調(diào)用攝像頭獲取個人圖片的實現(xiàn)方法
今天小編就為大家分享一篇關(guān)于Python基于opencv調(diào)用攝像頭獲取個人圖片的實現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
python socket網(wǎng)絡(luò)編程之粘包問題詳解
這篇文章主要介紹了python socket網(wǎng)絡(luò)編程之粘包問題詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

