码迷,mamicode.com
首页 > 其他好文 > 详细

Random numbers

时间:2014-08-14 19:49:49      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   os   io   for   ar   

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.

 bubuko.com,布布扣                      

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:

 bubuko.com,布布扣

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

Random numbers,布布扣,bubuko.com

Random numbers

标签:des   style   blog   http   os   io   for   ar   

原文地址:http://www.cnblogs.com/ryansunyu/p/3912862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!