首页 > 编程语言 > 详细

python随机数,随机选择……random

时间:2017-11-16 20:11:34      阅读:317      评论:0      收藏:0      [点我收藏+]
import random
from random import random, uniform, randint, randrange, choice, sample, shuffle

list = [‘jack‘, ‘rose‘, ‘tom‘, ‘jerry‘]
print(random())  # 随机小数 0.0 <= x < 1.0
print(uniform(1, 10))  # 随机小数 x, 1.0 <= x < 10.0
print(randint(1, 10))  # 随机整数 1 to 10, endpoints included
print(randrange(0, 101, 2))  # 随机0-101,步进2,也就是偶数
print(randrange(0, 101, 5))  # 随机0-101,步进5,也就是5的倍数
print(choice(‘abcdefghij‘))  # 随机字符串中的一个字符
items = [1, 2, 3, 4, 5, 6, 7]
print(items)
shuffle(items)  # 重新随机排序
print(items)
print(sample([1, 2, 3, 4, 5], 3))  # 随机选3个

  

python随机数,随机选择……random

原文:http://www.cnblogs.com/jackadam/p/7845883.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!