python 生成随机数

import random
import string
import time
from datetime import date,timedelta
class randoms():
    # 获取26个大小写字母
    letters = string.ascii_letters
    # 获取26个小写字母
    Lowercase_letters = string.ascii_lowercase
    # 获取26个大写字母
    Capital = string.ascii_uppercase
    # 获取阿拉伯数字
    digits = string.digits

1,随机生成指定位数的数字字母组合

    def code(self):
        # s是小写字母和数字的集合
        s = randoms.Lowercase_letters+randoms.digits
        #生成28位小写和数字的集合,并将列表转字符串
        code=‘‘.join(random.sample(s,28))
        print(‘随机code:%s‘%code)
        return code

或者

from datetime import date,timedelta
    def r_string(self):#生成随机字符串
        data="1234567890zxcvbnmlkjhgfdsaqwertyuiop"
        #用时间来做随机播种
        random.seed(time.time())
        #随机选取数据
        sa=[]
        for i in range(20):
            sa.append(random.choice(data))
        salt="gp_"+‘‘.join(sa)
        print(salt)
        # return salt

2,随机生成不包含某些字母或数字的字符

    def tax_code(self):
        list_1=[‘I‘,‘O‘,‘Z‘,‘S‘,‘V‘]
        list_2=[]
        for j  in  randoms().Capital:
            if j not in list_1:
                list_2.append(j)
        s =‘‘.join(list_2)  + randoms().digits
        tax_code=‘‘.join(random.sample(s,18))
        print(‘随机税号:%s‘%tax_code)
        return tax_code

3,生成随机手机号,详见https://blog.csdn.net/z5622139/article/details/79197089

    def telephone(self):
        # 第二位数字
        second = [3, 4, 5, 7, 8][random.randint(0, 4)]
        # 第三位数字
        third = {
            3: random.randint(0, 9),
            4: [5, 7, 9][random.randint(0, 2)],
            5: [i for i in range(10) if i != 4][random.randint(0, 8)],
            7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)],
            8: random.randint(0, 9),
        }[second]
        # 最后八位数字
        suffix = random.randint(10000000, 99999999)

        # 拼接手机号
        telephone="1{}{}{}".format(second, third, suffix)
        print(‘手机号:%s‘%telephone)
        return telephone

生成随机手机号方法2,详见https://www.cnblogs.com/jiuyigirl/p/7144725.html

    def create_telephone(self):
        telephone=random.choice([‘139‘, ‘188‘, ‘185‘, ‘136‘, ‘158‘, ‘151‘]) + "".join(
            random.choice("0123456789") for i in range(8))
        print(telephone)
        return telephone

函数使用补充说明

1,random是用于生成随机数的,我们可以利用它随机生成数字或者选择字符串,使用如下,详见https://www.cnblogs.com/jiuyigirl/p/7144725.html

random.random()    用于生成一个随机浮点数:range[0.0,1.0)

random.uniform(a,b)    用于生成一个指定范围内的随机浮点数,a,b为上下限,只要a!=b,就会生成介于两者之间的一个浮点数,若a=b,则生成的浮点数就是a

random.randint(a,b)    用于生成一个指定范围内的整数,a为下限,b为上限,生成的随机整数a<=n<=b;若a=b,则n=a;若a>b,报错

random.randrange([start], stop [,step])    从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1

random.choice(sequence)    从序列中获取一个随机元素,参数sequence表示一个有序类型,并不是一种特定类型,泛指list,tuple,字符串等

random.shuffle(x[,random])    用于将一个列表中的元素打乱

random.sample(sequence,k)    从指定序列中随机获取k个元素作为一个片段返回,sample函数不会修改原有序列

2,Python中有join()和os.path.join()两个函数,具体作用如下:
    join():    连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串
    os.path.join():  将多个路径组合后返回

原文地址:https://www.cnblogs.com/xiaozeng6/p/10992215.html

时间: 2024-08-02 03:21:20

python 生成随机数的相关文章

【python】【转】Python生成随机数的方法

如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模块中最常用的几个函数的关系,希望你会有所收获,以下就是这篇文章的介绍.random.random()用于生成用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限.如果a > b,则生成随机数n: a <= n <= b.如果 a <b, 则 b <= n <= a.print random.uniform(10,

python生成随机数:uniform(), randint(), gauss(), expovariate()

目录 22.python生成随机数:uniform(), randint(), gauss(), expovariate() 22.1 模块:random内建模块,伪随机数生成器 22.2 播种随机数,即用随机数种子seed控制随机数 22.3 在已知的范围内生成随机数,例如[2, 5],那就可以random.random()*3 + 2, uniform(2,5), randint(2,5) 22.4 从列表中随机选择一个值:choice(), choices() 22.5 shuffling

Python 生成随机数函数和加密函数(MD5)

内容来自debugtalk import hashlib import random import string def gen_random_string(str_len): '''生成指定长度的随机数函数''' return ''.join( random.choice(string.ascii_letters + string.digits) for _ in range(str_len)) def gen_md5(*args): '''MD5加密''' return hashlib.md

python生成随机数、随机字符串

import randomimport string # 随机整数:print random.randint(1,50) # 随机选取0到100间的偶数:print random.randrange(0, 101, 2) # 随机浮点数:print random.random()print random.uniform(1, 10) # 随机字符:print random.choice('[email protected]#$%^&*()') # 多个字符中生成指定数量的随机字符:print r

[ Python入门教程 ] Python生成随机数模块(random)使用方法

1.生成指定范围内的随机整数 >>> random.randint(0,100) 28 >>> random.randint(0,100) 36 >>> random.randint(0,100) 71 2.指定序列中随机选1个元素 >>> random.choice(range(1,100)) 10 >>> random.choice(range(1,100)) 36 >>> random.cho

使用Python生成随机数

另一种方式是把包含整数0~n-1的数组顺序打乱,然后把前m个元素排序输出: for i in range(0, m): swap(i, randint(i, n-1))

Python:time模块&amp;序列化&amp;md5&amp;生成随机数&amp;反射

time模块:>>> import time >>> time.time <built-in function time> >>> time.time() 1473837803.320634 >>> time.localtime() time.struct_time(tm_year=2016, tm_mon=9, tm_mday=14, tm_hour=15, tm_min=23, tm_sec=58, tm_wday=2

【转载】python 模块 - random生成随机数模块

http://blog.csdn.net/pipisorry/article/details/39086463 随机数种子 要每次产生随机数相同就要设置种子,相同种子数的Random对象,相同次数生成的随机数字是完全相同的: random.seed(1) 这样random.randint(0,6, (4,5))每次都产生一样的4*5的随机矩阵 关于种子的介绍可参见[Java - 常用函数Random函数] Python标准库random模块 (生成随机数模块) random.random() r

Python生成测试数据

本文出自:http://blog.csdn.net/svitter 生成1~10的随机数1000个: import random fp = open("test", 'w'); for i in range(1, 1000): a = random.randint(1,10) fp.write(str(a)+"\n"); fp.close(); 注意:写入文件的不会在最后写入,而是重新写文件. Python生成测试数据,布布扣,bubuko.com