Random generation of verification codes

巴扎黑
Release: 2017-07-17 15:59:45
Original
1668 people have browsed it

1. With the help of the list

import random

def random_code():
    random_list = []for i in range(4):
        ra = random.randrange(4)if ra == i:
            random_list.append(chr(random.randrange(97,122)))else:
            random_list.append(str(random.randrange(0,9)))
    code = "".join(random_list)return code

result = random_code()
print(result)
Copy after login

The above code realizes the function of random verification code, but it can only generate Random verification code of lowercase letters; and we also used the list to complete it. The splicing of strings plus the list and the string jion() method generated a random verification code. Let's try the method of generating uppercase verification code as well.

import random

def random_code():
    random_list = []for i in range(4):
        ra = random.randrange(4)if ra == i:
            random_list.append(chr(random.randrange(97,122)))
        elif ra > i:
            random_list.append(str(random.randrange(0,9)))else:
            random_list.append(chr(random.randrange(65,90)))
    code = "".join(random_list)return code

result = random_code()
print(result)
Copy after login

Since there are three relationships between data, greater than, less than, and equal to, by analyzing each situation, three random verification codes can be generated. ; When it is equal to, a lowercase verification code is generated, when it is greater than a number, a number is generated, when it is less than, an uppercase letter verification code is generated, thus realizing the common form of verification code we use on websites.

2. String splicing

import random

def verification_code():
    code = ""for i in range(1,5):
        ra = random.randint(1,4)if ra == i:string = chr(random.randrange(97,122))
        elif ra > i:string = chr(random.randrange(65,90))else:string = str(random.randint(0,9))
        code += stringreturn code

result = verification_code()
print(result)
Copy after login

This method is to use string splicing The generated random verification code is actually the same in essence as the above methods. The first method may be more efficient, but they all realize the function of the random verification code.

2. Method of generating bill serial number

import datetime,time

def serial_number():
    serial = "{0}{1}".format(datetime.datetime.now().strftime("%Y%m%d%H%M%S"),str(int(time.time())))return serial

message = serial_number()
print(message)
Copy after login

The bill serial number is composed of the datetime module and the time module, because the serial number is the time serial number of the purchase at that time, and it will never be repeated.

3. Use the range method

import random
def generate_verification_code(len=6):
    ''' 随机生成6位的验证码 '''
    # 注意: 这里我们生成的是0-9A-Za-z的列表,当然你也可以指定这个list,这里很灵活
    # 比如: code_list = ['P','y','t','h','o','n','T','a','b'] # PythonTab的字母
    code_list = [] 
    for i in range(10): # 0-9数字
        code_list.append(str(i))
    for i in range(65, 91): # 对应从“A”到“Z”的ASCII码
        code_list.append(chr(i))
    for i in range(97, 123): #对应从“a”到“z”的ASCII码
        code_list.append(chr(i))
    myslice = random.sample(code_list, len)  # 从list中随机获取6个元素,作为一个片断返回
    verification_code = ''.join(myslice) # list to string
    return verification_code
Copy after login


The above is the detailed content of Random generation of verification codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!