python的随机模块–random
random.random- 模块名称.模块中的函数
random.uniformrandom.randintrandom.choicerandom.samplerandom.randrange
random.random
- 随机返回0~1之间的浮点数
random.uniform
- 产生一个a,b区间的随机浮点数
random.randint
- 产生一个a, b区间的随机整数
random.choice
- 返回对象中的一个随机元素
random.sample
随机返回对象中指定的元素
# 即个数
random.randrange
- 获取区间内的一个随机数
random.randrange(0, 100, 1)# 区间,步长random.choice(range(0, 100, 1))
代码
| # coding:utf-8 | |
| import random | |
| gifts = ['iphone', 'ipad', 'car', 'tv'] | |
| def chioce_gifts(): | |
| gift = random.choice(gifts) | |
| print('你得到了%s' % gift) | |
| def chioce_gift_new(): | |
| count = random.randrange(0, 100, 1) | |
| if 0 <= count <= 50: | |
| print('你中了一个iphone') | |
| elif 50 < count <= 70: | |
| print('你中了一个ipad') | |
| elif 70 < count < 90: | |
| print('你中了一个tv电视') | |
| elif count >= 90: | |
| print('恭喜你中了一辆小汽车') | |
| if __name__ == '__main__': | |
| # chioce_gifts() | |
| chioce_gift_new() | |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ZkeqのCoding日志!
评论





