python random 模組

來源:互聯網
上載者:User

標籤:傳遞   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 模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.