Most computer programs do the same thing every time they execute, given the same inputs, so they are said to be deterministic. Deterministic is usually a good thing, since we expect the same calculation to yield the same result. For some applications, though, we want the computer to be unpredictable. Games are an obvious example, but there are more.

Making a program truly nondeterministic turns out to be not so easy, but there are ways to make it at least seem nondeterministic. One of them is to use algorithms that generate pseudorandom numbers. Pseudorandom numbers are not truly random because they are generated by a deterministic computation, but just by looking at the numbers it is all but impossible to distinguish them from random. The random module provides functions that generate pseudorandom numbers.

The function random returns a random float between 0.0 and 1.0. Each time you call random, you get the next number in a long series.

The function randint takes parameters low and high and returns an integer between low and high (including both). And to choose an element from a sequence at random, you can use choice:

The random module also provides functions to generate random values from continuous distributions including Gaussian, exponential, gamma, and a few more.

from Thinking in Python

最新文章

  1. postman发送带cookie的http请求
  2. MSXML使用教程
  3. PureBasic 集成Form设计器的使用
  4. java 笔记(3) —— 动态代理,静态代理,cglib代理
  5. hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
  6. F1
  7. Redis HyperLogLog
  8. Dynamics CRM 2013 初体验(5):Business Rule
  9. 如何设置eclipse在默认模式下打开文件
  10. Python3基础 用while 循环求解 一个整数的阶乘
  11. js,jQuery和DOM操作的总结(一)
  12. 书籍--嵌入式C语言
  13. 第三篇:Python字符编码
  14. 好几个div(元素)找到最后一个
  15. SpringBoot热部署-解决方案
  16. NET(C#):关于正确读取中文编码文件
  17. pdf 移除密码 去除水印 批量去除水印 编辑文字 批量替换文字
  18. SQL Server 2008重新保存表时出错
  19. bzoj千题计划283:bzoj4516: [Sdoi2016]生成魔咒(后缀数组)
  20. 框架之Tornado(简单介绍)

热门文章

  1. HDU 4856 Tunnels(BFS+状压DP)
  2. SQL编码中注意的性能问题
  3. Android 开发之集成百度地图的定位与地图展示
  4. hdu2546 饭卡 01-背包问题
  5. 杭电3501Calculation 2 欧拉函数
  6. ThinkPHP5.0框架开发--第4章 TP5.0路由
  7. VC下加载多种格式图片的方法总结IPicture, CxImage, CImage(AtlImage), CPictureEx
  8. 9.variant move function change_cast
  9. Codeforces 703D Mishka and Interesting sum 离线+树状数组
  10. luogu P1375 小猫(卡特兰数)