random模組產生隨機手機驗證碼

來源:互聯網
上載者:User

標籤:bit   重要   開始   import   if語句   指定   序列   logs   span   

import random

# print(random.random()) #浮點數,0-1,無法指定range
# print(random.uniform(1,5)) #浮點數,1-5
# print(random.randint(1,100)) #1-99隨機
# print(random.randrange(1,5)) #1-4隨機
# print(random.choice(‘hello‘)) #取1位
# print(random.sample(‘hello‘,2)) #隨機取2位

# x=[1,2,3,4,5,6]
# print(x)
# print(random.shuffle(x)) #隨機序列

 1 checkcode=‘‘    #剛開始為空白 2  3 for i in range(4):     #0-3, 輪詢4次,相當於驗證碼長度4位 4     bit=random.randrange(0,4)     #隨機數0-3 5     if bit==i:               #如正好在第1次出來的隨機數也為1 6         tmp=chr(random.randint(65,90))   #則將隨機數轉成ASCII碼A-Z,65=A,90=Z 7     else: 8         tmp=random.randint(0,9)     #如正好在第1次出來的隨機數不為1,則隨機產生0-9的隨機數 9     checkcode+=str(tmp)     #if語句輪詢4次,每次將tmp得到的值賦給checkcode10 11 print(checkcode)12 #9ZM9

編程思路太TM重要了!!!

 

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.