转自:http://www.cnblogs.com/yd1227/archive/2011/03/18/1988015.html

随机整数:
>>> import random
>>> random.randint(0,99)
21

随机选取0到100间的偶数:
>>> import random
>>> 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
random.sample('abcdefghij',3) 
['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'] )
'lemon'

洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]

最新文章

  1. 11、Linq的使用
  2. PRINCE2七大原则(1)
  3. 动态改变QSS
  4. 责任链模式(Chain of Responsibility Pattern)
  5. java-mina(nio 框架)
  6. 关于在MDK4.5以上版本不能使用JLINK V8的解决办法
  7. [LeetCode]题解(python):070-Climbing Stairs
  8. Apache+php+mysql+phpadmin搭建
  9. AFNetworking2.0和AFNetworking3.0 的HTTPS的配置
  10. 流程控制------if else分支语句
  11. [Spark内核] 第37课:Task执行内幕与结果处理解密
  12. 仓储repository概念
  13. video标签,在移动端获取第一帧作为展示
  14. linux系统下完全卸载Jenkins
  15. 批量找注入 python3+sqlmap结合
  16. 设计模式のChainOfResponsibilityPattern(责任链模式)----行为模式
  17. [No0000194]聊聊 Chrome DevTools 中你可能不知道的调试技巧
  18. 19 中山重现赛 1002 triangle
  19. 分享:android图片浏览器—类微信朋友圈相片浏览【android代码下载】
  20. hadoop源码学习(-)

热门文章

  1. iOS 内存管理(转载)
  2. 联合约束 CONCAT()
  3. SpringBoot 配置文件 YML/Profile
  4. MapReduce自定义InputFormat和OutputFormat
  5. 策略(strategy)模式
  6. react 日期
  7. django cookie 提供的功能 参数
  8. Oracle trigger 触发器
  9. 在PL/SQL中如何让程序暂停几秒钟
  10. 配置支持Basler的API函数的开发环境