Python: Random

 

# encoding: utf-8
# 版权所有 2023 涂聚文有限公司
# 许可信息查看:
# 描述:https://docs.python.org/3/library/string.html
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# Datetime  : 2023/12/17 18:16
# User      : geovindu
# Product   : PyCharm
# Project   : pyBaiduAi
# File      : RandomHelper.py
# explain   : 学习




import random
import string

class RandomHelper(object):
    """
    随机字符串
    """

    @staticmethod
    def getString(length:int):
        """
        生成随机的字符串(大小写英文字母、数字组成)
        :param length
        :return:
        """
        st=''
        randomstr =st.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
        return  randomstr

    @staticmethod
    def getStringSample(length: int):
        """
        生成随机的无重复字符的字符串(大小写英文字母、数字组成)
        :param length
        :return:
        """
        st = ''
        randomstr = st.join(random.sample(string.ascii_letters + string.digits) for _ in range(length))
        return randomstr

    @staticmethod
    def getCumstmer(length:int):
        """
        自定义字符串随机字符串
        :param length: 长度
        :return:
        """
        st = ''
        #1
        #randomstr =''.join(random.sample(['z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'], length))
        #2
        st = random.sample('abcdefghijklmnopqrstuvwxyz!@#$%^&*',length)
        randomstr=''.join(st)
        return randomstr

    @staticmethod
    def getCumstmerTwo(length:int):
        """
        自定义字符串随机字符串
        :param length: 长度
        :return:
        """
        randomstr = "23456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*+="
        st = []
        for i in range(length):
            st.append(random.choice(randomstr))
        result = ''.join(st)
        return result

    @staticmethod
    def getCumstmerChinese(length:int):
        """
        自定义中文字符串随机字符串
        :param length: 长度
        :return:
        """
        randomstr =['剪', '石', '布','中','国','涂','聚','文','学','习']
        st = []
        for i in range(length):
            st.append(random.choice(randomstr))
        result = ''.join(st)
        return result

  

posted @ 2023-12-17 20:49  ®Geovin Du Dream Park™  阅读(7)  评论(0编辑  收藏  举报