Python生成隨機數(shù)字和字符詳情
random庫
random是python自帶庫,使用前導(dǎo)入 import random
即可,無需安裝。
舉例:
import random print(random.random()) # 0.8564557389763434 print(random.uniform(0, 100)) # 78.19630268831175 print(random.randint(0, 100)) # 45 print(random.choice('12345')) # 4 print(random.choice([1,2,3,4,5])) # 1 print(random.choice((1,2,3,4,5))) # 5 print(random.sample('12345', 3)) # ['1', '5', '2'] print(random.sample([1,2,3,4,5], 3)) # [5, 4, 1] print(random.sample((1,2,3,4,5), 3)) # [4, 5, 1] list = [1, 2, 3, 4, 5] random.shuffle(list) print(list) # [3, 5, 4, 1, 2] print(random.randrange(0,20,5)) # 10
生成一個指定長度的隨機字符串
使用random.randint(0,n)
思路:定義一個基礎(chǔ)字符序列,要產(chǎn)生多長的字符串就循環(huán)調(diào)用多少次random.randint(0,n),n=基礎(chǔ)字符序列的長度,在這個序列中選擇隨機位置的字符,然后進(jìn)行拼接返回。
import random import string def generate_random_str(randomlength=16): """ 生成一個指定長度的隨機字符串 """ random_str ='' # 使用string庫中的字母、數(shù)字和標(biāo)點符號組成一個基礎(chǔ)字符序列 base_str = string.ascii_letters + string.digits + string.punctuation length =len(base_str) -1 for i in range(randomlength): random_str +=base_str[random.randint(0, length)] return random_str print(generate_random_str(30)) # T`^7:C?z4h0qd!JpO:MFNB2_GL>dXm
使用random.choice()隨機選擇
import random import string def generate_random_str(randomlength=16): """ 生成一個指定長度的隨機字符串 """ # 使用string庫中的字母、數(shù)字和標(biāo)點符號拼接程序列 str_list = [random.choice(string.ascii_letters + string.digits + string.punctuation) for i in range(randomlength)] random_str = ''.join(str_list) return random_str print(generate_random_str(30)) # ')8D@Bu&\1ltf!}HUcI-.;^>Vy[n|V
使用random.simple()隨機選擇
import random import string def generate_random_str(randomlength=16): """ 生成一個指定長度的隨機字符串 """ # 使用string庫中的字母、數(shù)字和標(biāo)點符號組成一個基礎(chǔ)字符序列 base_str = string.ascii_letters + string.digits + string.punctuation str_lst = random.sample(base_str, randomlength) #randomlength<=lenth(base_str) random_str = ''.join(str_lst) return random_str
Faker庫
使用Faker庫可生成各種各樣的偽數(shù)據(jù)(姓名、地址、郵箱、電話、密碼、一段文字、一句話等)。
安裝:pip install faker
導(dǎo)入:from faker import Faker
示例:
from faker import Faker fk = Faker(locale='zh-Cn') # 姓名 print(fk.name()) # 劉娜 # 身份證 print(fk.ssn()) # 141022195412020845 # 手機號 print(fk.phone_number()) # 18226186826 # 郵箱 print(fk.email()) # zoujie@example.org
個人信息類
數(shù)字類
文本、加密類
日期類
地理信息類
from faker import Faker fk = Faker(locale='zh-Cn') # 從[0,9]中隨機獲取7位數(shù)字(可重復(fù)) print([fk.random_digit() for i in range(7)]) # [2, 0, 7, 8, 2, 3, 2] # 從[0,9]中隨機獲取7位數(shù)字(不可重復(fù)) print([fk.unique.random_digit() for i in range(7)]) # [4, 9, 3, 6, 8, 5, 2]
到此這篇關(guān)于Python生成隨機數(shù)字和字符詳情的文章就介紹到這了,更多相關(guān)Python生成隨機數(shù)字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python可變參數(shù)會自動填充前面的默認(rèn)同名參數(shù)實例
今天小編就為大家分享一篇Python可變參數(shù)會自動填充前面的默認(rèn)同名參數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11python中plot實現(xiàn)即時數(shù)據(jù)動態(tài)顯示方法
這篇文章主要為大家詳細(xì)介紹了python中plot實現(xiàn)即時數(shù)據(jù)動態(tài)顯示方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06Python如何使用logging為Flask增加logid
這篇文章主要介紹了Python如何使用logging為Flask增加logid,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03Pytorch中的數(shù)據(jù)轉(zhuǎn)換Transforms與DataLoader方式
這篇文章主要介紹了Pytorch中的數(shù)據(jù)轉(zhuǎn)換Transforms與DataLoader方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02python多進(jìn)程執(zhí)行方法apply_async使用說明
這篇文章主要介紹了python多進(jìn)程執(zhí)行方法apply_async使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03