Python 基础 - 4.3 random模块
生活随笔
收集整理的這篇文章主要介紹了
Python 基础 - 4.3 random模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
程序中有很多地方需要用到隨機字符,比如登錄網站的隨機驗證碼,通過random模塊可以很容易生成隨機字符串
>>> random.randomrange(1,10) #返回1-10之間的一個隨機數,不包括10 >>> random.randomint(1,10) #返回1-10之間的一個隨機數,包括10>>> random.randrange(0, 100, 2) #隨機選取0到100間的偶數>>> random.random() #返回一個隨機浮點數 >>> random.choice('abce3#$@1') #返回一個給定數據集合中的隨機字符 '#'>>> random.sample('abcdefghij',3) #從多個字符中選取特定數量的字符 ['a', 'd', 'b'] #生成隨機字符串 >>> 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] ?轉載于:https://www.cnblogs.com/caimengzhi/p/8352942.html
總結
以上是生活随笔為你收集整理的Python 基础 - 4.3 random模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 已经升级了iOS14GM 版,明天还能升
- 下一篇: python下载大文件