python 随机方法

# 随机函数

import random

#随机整数:下限必须小于上限
i = random.randint(0, 9)
# print(i)

#随机偶数
o = random.randrange(0, 9, 2)
# print(o)

#随机浮点数,小于0<1
f1 = random.random()
# print(f1)

#随机在区间内的浮点数
f2 = random.uniform(1, 10)
# print(f2)

#随机字符,指定字符串内的单个字符
d = random.choice('abc')
# print(d)

#随机选一个字符串
s = random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'])
# print(s)

#随机字符,指定字符串内的多个字符,返回list
z = random.sample('abcdefghij',3)
# print(z)

#随机一个新的字符串,在列表中选出多个字符组成新字符串
a = ('').join(random.sample(['a','b','c','d'], 2))
# print(a)

#重新排序
items = [1, 2, 3, 4, 5, 6]
random.shuffle(items)
print(items)


#实际使用
    list = []
    #通过for训话把获取的id添加到list内
    for i in range(0, len(req1['data'])):
        a = req1['data'][i]['id']
        list.append(a)
    # random.choice这个方法是用来获取随机数的
    ran = random.choice(list)

参考:https://www.cnblogs.com/mfryf/p/4556007.html

原文地址:https://www.cnblogs.com/lijinglj/p/9642300.html