標籤:傳遞 ascii font amp col 列表 dom ros git
import random # 隨機數import string
隨機小數
print(random.random())0.8681861054821751
在1-5範圍 隨機列印
print(random.randint(1,5))1
和randint區別 在1-4範圍 隨機列印
print(random.randrange(1,5))3
在這個範圍內 隨機拿出5個數出來,以list方式列印
print(random.sample(range(100),5))[50, 92, 29, 94, 30]print(random.sample(‘abcdef‘,5))[‘b‘, ‘d‘, ‘f‘, ‘e‘, ‘c‘]
列印0-9數字
print(string.digits)0123456789
列印a-z
print(string.ascii_letters)abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
隨機拿出0-9 a-z A-Z 裡面隨機數 以字串格式列印
str_source = string.ascii_letters + string.digitsprint(‘‘.join(random.sample(str_source,5)))Dzn5w
choice 隨機傳遞序列元祖,字串,列表 裡面隨機數
print(random.choice([1,3,5]))5print(random.choice(‘helo‘))e
洗牌功能
l = [1,2,3,4,5,6,7,8]print(l)[1, 2, 3, 4, 5, 6, 7, 8]random.shuffle(l)print(l)[6, 2, 5, 3, 4, 8, 1, 7]
隨機數驗證碼小程式
import randomcheckcode = ‘‘for i in range(5): current = random.randrange(0,5) if current != i: temp = chr(random.randint(65,90)) elif i > 2: temp = chr(random.randint(97,122)) else: temp = random.randint(0,9) checkcode += str(temp)print(checkcode)
python random 模組