Python 基础 - 4.3 random模块
生活随笔
收集整理的這篇文章主要介紹了
Python 基础 - 4.3 random模块
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
程序中有很多地方需要用到隨機(jī)字符,比如登錄網(wǎng)站的隨機(jī)驗(yàn)證碼,通過(guò)random模塊可以很容易生成隨機(jī)字符串
>>> random.randomrange(1,10) #返回1-10之間的一個(gè)隨機(jī)數(shù),不包括10 >>> random.randomint(1,10) #返回1-10之間的一個(gè)隨機(jī)數(shù),包括10>>> random.randrange(0, 100, 2) #隨機(jī)選取0到100間的偶數(shù)>>> random.random() #返回一個(gè)隨機(jī)浮點(diǎn)數(shù) >>> random.choice('abce3#$@1') #返回一個(gè)給定數(shù)據(jù)集合中的隨機(jī)字符 '#'>>> random.sample('abcdefghij',3) #從多個(gè)字符中選取特定數(shù)量的字符 ['a', 'd', 'b'] #生成隨機(jī)字符串 >>> import string >>> ''.join(random.sample(string.ascii_lowercase + string.digits, 6)) '4fvda1' #洗牌 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> random.shuffle(a) >>> a [3, 0, 7, 2, 1, 6, 5, 8, 9, 4] ?轉(zhuǎn)載于:https://www.cnblogs.com/caimengzhi/p/8352942.html
總結(jié)
以上是生活随笔為你收集整理的Python 基础 - 4.3 random模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 已经升级了iOS14GM 版,明天还能升
- 下一篇: python下载大文件