Python 3.6 生成随机码

四种方法,生成随机码

最近项目需要随机码,查了查找到四种方法,在这里罗列出来,以备不时之需

由于RHEL 8 只支持Python 3.6,所以2.7的就不研究了(PS:毫无歧视2.7的意思,单纯的为了以后安装CentOS 8 做准备)

# vim 666.py
import random
import string
seed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
sa = []
for i in range(123):
    sa.append(random.choice(seed))
salt1=''.join(sa)
print(salt1)

salt2 = ''.join(random.sample(string.ascii_letters + string.digits, 60))
print(salt2)

salt3 = random.randint(100000,999999)
print(salt3)

salt4 = ""
for i in range(20):       #循环20次,从0到19
    salt5 = random.randrange(0,6)     #这个随机数为2或者4时,生成一个1到9的随机数放到字符串中
    if salt5 == 2 or salt5 == 4:
        salt4 = salt4 + str(random.randrange(0,10))
    else:
        salt6 = chr(random.randrange(65,122))
    salt4 = salt4 + salt6
print(salt4)
# python3.6 666.py

效果如下:

6 评论

  1. Today, I went to the beachfront with my children. I found a sea
    shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She
    put the shell to her ear and screamed. There was
    a hermit crab inside and it pinched her ear. She never wants to
    go back! LoL I know this is entirely off topic but
    I had to tell someone!

  2. You actually make it appear so easy along with your presentation however I
    to find this matter to be really something which I feel I
    might never understand. It sort of feels too complex and extremely huge for me.

    I am taking a look ahead for your next submit,
    I’ll attempt to get the hold of it!

  3. Hey there are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and create my own. Do you need any
    coding knowledge to make your own blog? Any help would
    be really appreciated!

留下评论

error: Content is protected !!