【python】生成*不重复*随机n<=10位数字与字母组合(创建随机)

wuchangjian2021-11-05 01:04:34编程学习

1.环境

windows/ubuntu
python3.6

2.实现

下面代码包含:

1)随机数字+字母组合

2)随机字母组合

3)随机字母组合

4)随机不重复数字+字母组合

5)随机不重复字母组合

6)随机不重复字母组合

# -*- coding: UTF-8 -*-
'''
@author: mengting gu
@contact: 1065504814@qq.com
@time: 2020/11/3 下午9:04
@file: random_num.py
@desc:
    Get the combination of n numbers and upper and lower case letters.
'''

import random

n = 10

def v_code_nums_letters(n=6):
        """
            Returns:
                ret:random six num and letter
        """
        ret = ""
        for i in range(n):
            num = random.randint(0, 9)
            letter = chr(random.randint(97, 122))  # 取小写字母
            Letter = chr(random.randint(65, 90))  # 取大写字母
            s = str(random.choice([num, letter, Letter]))
            ret += s
        return ret

def v_code_letters(n=6):
        """
            Returns:
                ret:random six letter
        """
        ret = ""
        for i in range(n):
            letter = chr(random.randint(97, 122))  # 取小写字母
            Letter = chr(random.randint(65, 90))  # 取大写字母
            s = str(random.choice([letter, Letter]))
            ret += s
        return ret

def v_code_nums(n=6):
        """
            Returns:
                ret:random six num
        """
        ret = ""
        for i in range(n):
            num = random.randint(0, 9)
            s = str(random.choice([num]))
            ret += s
        return ret


def v_code_nums_letters_norep(n=6):
        """
            Returns:
                ret:random six num and letter
        """
        num = random.sample(range(0, 10), n)
        letter = random.sample(range(97, 122), n)  # 取小写字母
        Letter = random.sample(range(65, 90), n)  # 取大写字母
        ret = ""
        for i in range(n):
            s = str(random.choice([num[i], chr(letter[i]), chr(Letter[i])]))
            ret += s
        return ret

def v_code_letters_norep(n=6):
        """
            Returns:
                ret:random six letter
        """
        letter = random.sample(range(97, 122), n)  # 取小写字母
        Letter = random.sample(range(65, 90), n)  # 取大写字母
        ret = ""
        for i in range(n):
            s = str(random.choice([chr(letter[i]), chr(Letter[i])]))
            ret += s
        return ret

def v_code_nums_norep(n=6):
        """
            Returns:
                ret:random six num
        """
        num = random.sample(range(0, 10), n)
        ret = ""
        for i in range(n):
            s = str(num[i])
            ret += s
        return ret

print("v_code_nums_letters result: " + v_code_nums_letters())
print("v_code_letters result: " + v_code_letters())
print("v_code_nums result: " + v_code_nums_norep())

print("v_code_nums_letters_norep result: " + v_code_nums_letters_norep())
print("v_code_letters_norep result: " + v_code_letters_norep())
print("v_code_nums_norep result: " + v_code_nums_norep())

3.结果

运行:

python random_num.py

得到下面结果: 

v_code_nums_letters result: RCJexk7N6W
v_code_letters result: SuTvHHomgJ
v_code_nums result: 0089680583
v_code_nums_letters_norep result: ok0lEBTnh9
v_code_letters_norep result: hUqFiBYvcK
v_code_nums_norep result: 2061734859

相关文章

Java单例模式七种实现

一、Java单例模式实现可分为七种比较经典的模式,从实现方式可分为懒汉模式...

1005CallatzPlus

# 观摩了一下 大佬代码的优化 # eval 函数自动将输入转为int n =...

刷题日志——链表篇(12.5)

今日继续,链表题刷起来。 目录 1.相交链表 2.反转链表...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。