Python__random模块
生活随笔
收集整理的這篇文章主要介紹了
Python__random模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#Author wangmengzhu
import random
print(random.random())##隨機生成大于0且小于1之間的小數
print(random.randint(1,3))#大于等于1且小于等于3之間的整數
print(random.randrange(1,3))#[1,3)
print(random.choice([1,'23',[4,5]]))#隨機選擇一個元素
print(random.sample([1,'23',[4,5]],2))#列表元素任意選擇兩個組合
print(random.uniform(1,3))#大于1小于3的小數
item = [1,3,5,7,9]
random.shuffle(item)#打亂item的順序,相當于洗牌
print(item)
#生成隨機驗證碼
def make_code(n):
res = ''
for i in range(n):
s1 = str(random.randint(0,9))
s2 = chr(random.randint(65,90))
res += random.choice([s1,s2])
return res
print(make_code(10))
import random
print(random.random())##隨機生成大于0且小于1之間的小數
print(random.randint(1,3))#大于等于1且小于等于3之間的整數
print(random.randrange(1,3))#[1,3)
print(random.choice([1,'23',[4,5]]))#隨機選擇一個元素
print(random.sample([1,'23',[4,5]],2))#列表元素任意選擇兩個組合
print(random.uniform(1,3))#大于1小于3的小數
item = [1,3,5,7,9]
random.shuffle(item)#打亂item的順序,相當于洗牌
print(item)
#生成隨機驗證碼
def make_code(n):
res = ''
for i in range(n):
s1 = str(random.randint(0,9))
s2 = chr(random.randint(65,90))
res += random.choice([s1,s2])
return res
print(make_code(10))
轉載于:https://www.cnblogs.com/wangmengzhu/p/7306092.html
總結
以上是生活随笔為你收集整理的Python__random模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 1540 Tunnel Warf
- 下一篇: JVM的常用配置参数