py 随机数收藏
来源:互联网 发布:linux broken pipe 编辑:程序博客网 时间:2024/06/03 20:34
http://blog.csdn.net/zhenyu5211314/article/details/18199017
Python生成随机数
随机整数:
>>> import random
>>> random.randint(0,99)
21
>>> random.randrange(0, 101, 2)
42
随机浮点数:
>>> import random
>>> random.random()
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
随机字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
>>> import random
洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
>>> import random
>>> random.randint(0,99)
21
随机选取0到100间的偶数:
>>> random.randrange(0, 101, 2)
42
随机浮点数:
>>> import random
>>> random.random()
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
随机字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多个字符中选取特定数量的字符:
>>> import random
['a', 'd', 'b']
多个字符中选取特定数量的字符组成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
随机选取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
0 0
- py 随机数收藏
- py
- py
- py
- py
- py
- py
- py
- Py
- 随机数
- 随机数
- 随机数。。。
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- Eclipse的使用
- 浅谈职场五傻规则
- HTTP请求的GET与POST方式的区别
- Spring Boot学习记录–thymeleaf模板
- junit问题
- py 随机数收藏
- springmvc返回json
- char 与 unsigned char的本质区别
- Java web实用视频教程(转)
- 如何实现可迭代对象和迭代器对象
- get方式带参数中文出现乱码
- Android中dp与px的对应关系
- Thread类包含start()和run()方法
- Boudica 120