码迷,mamicode.com
首页 > 编程语言 > 详细

python 随机方法

时间:2018-09-13 20:10:32      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:区间   shuffle   偶数   随机函数   com   col   参考   tps   单个字符   

# 随机函数

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

 

python 随机方法

标签:区间   shuffle   偶数   随机函数   com   col   参考   tps   单个字符   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!