python学习:随机数的产生,随机数拼接字在脚本中的应用

 学习random的时候,看到一份表格觉得不错,转载记录到自己的笔记中:

random以及它们在numpy.random中对应的函数应该会很有帮助:

 注意:NumPy专门用于构建和操作大型多维数组。如果您只需要一个值,那么random就足够了,可能还会更快。对于小序列,random也可能更快,因为NumPy会带来了一些额外开销。

我用到的是随机数最简单的几个功能:

在Python中用于生成随机数的模块是random,在使用前需要import。
random.random():生成一个0-1之间的随机浮点数
random.randint(a,b):生成[a,b]之间的整数,包含a,b
random.uniform(a,b):生成[a,b]之间的浮点数
random.choice(sequence):从特定序列的中随机取一个元素,这里的序列可以是字符列表,元组等。
例如:生成16位的随机数:
import random
str = ''
a=str.join(random.choice("0123456789") for i in range(16))
print(a)

为了写卡压测,用到拼接字和随机数,用到我的脚本里是这样实现的:
if __name__ == "__main__"or True:
   from task import Task #次数调用为自定义函数
   for i in range(100):
        SET = Task(cz_setcmds)
        SET.execute()
        if SET.is_success:
            IMSI = random.randint(460954110612179,460954110612199)
            str = ''
            ran1 = str.join(random.choice("0123456789")for j in range(32))
            iccid = str.join(random.choice("0123456789")for j in range(20))
            # print(IMSI,ran1,iccid)
            write_card = [
            Args(request='AT^HVSSSINFO="{0}",3,"{1}","{2}"'.format(IMSI,ran1,ran1),regex="^ OK ",stop_character="OK",timeout=15,can_skip=False),
            Args(request='AT^VSIMICCID={}'.format(iccid),regex="^ OK ",stop_character="OK",timeout=15,can_skip=False),
            Args(request="AT^SIMSEL=0",regex="^ OK ",can_skip=False),
            Args(request="AT^HVSST=11,1",regex="^ OK ",can_skip=False),
            Args(request="AT+CIMI",regex="^ {} OK ".format(IMSI),can_skip=False),
            Args(request="AT^VSIMICCID?",regex='^ ^VSIMICCID: "{}" OK '.format(iccid),can_skip=False),
            Args(request="AT+CFUN=1,1",regex="^ OK ",delay=20,can_skip=False)
            ]
            # print(write_card)
            Task(write_card).execute()
            print(i)
原文地址:https://www.cnblogs.com/wellons/p/14205455.html