random

获取范围随机数

获取 [0,10] 范围的随机数

1
random.randint(0, 10)

获取指定长度数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def get_random_num(**kwargs):
"""
获取指定长度的随机数字
"""
if not kwargs:
return '0'
if 'length' in kwargs and kwargs['length']:
length = int(kwargs['length'])
if length <= 0 or length > 50:
return '0'
range_start = 10 ** (length - 1)
range_end = (10 ** length) - 1
return str(random.randint(range_start, range_end))
if 'start' in kwargs and 'end' in kwargs:
if kwargs['start'] and kwargs['end']:
return random.randint(int(kwargs['start']), int(kwargs['end']))
return '0'
1
''.join(random.choices(string.ascii_uppercase + string.digits, k=6))