random 隨機數模組

來源:互聯網
上載者:User

標籤:順序   返回   port   ret   and   浮點   dom   自訂   之間   

import random# 隨機數模組print(random.random())  #0-1 不包括1隨機浮點數     print(random.randint(1,10)) # 1-10 包括1和10 的整數          print(random.randrange(1,10)) # 1-10包括1 不包括10的整數    print(random.sample(["aaa",["a","b"],3,4,5],2)) # 指定一個範圍並指定需要的隨機個數          ls = ["1","2","3","4"]random.shuffle(ls) # 打亂順序  洗牌 改的原有列表print(ls)       print(random.choice([1,2,3])) # 隨機選一個      print(random.choices([1,2,3,4,5,6,7],k=2)) # 隨機選指定個數      # 隨機驗證碼 長度自訂 包括0-9 A-Z a-zdef get_auth_code(length):    res = ""    for i in range(length):        a = random.randint(0,9)        b = chr(random.randint(65,90))        c = chr(random.randint(97,122))        s = random.choice([a,b,c])        res += str(s)    return resprint(get_auth_code(4))     #結果 : 4個[0-9 A-Z a-z] 的隨機值print(random.uniform(1,3)) # # 返回一個介於a和b之間的浮點數。如果a>b,則是b到a之間的浮點數。這裡的a和b都有可能出現在結果中。

 

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.