==random 模块==

        "Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."

        - John von Neumann, 1951

``random`` 模块包含许多随机数生成器. 

基本随机数生成器(基于 Wichmann 和 Hill , 1982 的数学运算理论) 可以通过很多方法访问,
如 [Example 2-29 #eg-2-29] 所示. ====Example 2-29. 使用 random 模块获得随机数字====[eg-2-29] ```
File: random-example-1.py import random for i in range(5): # random float: 0.0 <= number < 1.0
print random.random(), # random float: 10 <= number < 20
print random.uniform(10, 20), # random integer: 100 <= number <= 1000
print random.randint(100, 1000), # random integer: even numbers in 100 <= number < 1000
print random.randrange(100, 1000, 2) *B*0.946842713956 19.5910069381 709 172
0.573613195398 16.2758417025 407 120
0.363241598013 16.8079747714 916 580
0.602115173978 18.386796935 531 774
0.526767588533 18.0783794596 223 344*b*
``` 注意这里的 ``randint`` 函数可以返回上界,
而其他函数总是返回小于上界的值. 所有函数都有可能返回下界值. [Example 2-30 #eg-2-30] 展示了 ``choice`` 函数, 它用来从一个序列里分拣出一个随机项目.
它可以用于列表, 元组, 以及其他序列(当然, 非空的). ====Example 2-30. 使用 random 模块从序列取出随机项====[eg-2-30] ```
File: random-example-2.py import random # random choice from a list
for i in range(5):
print random.choice([1, 2, 3, 5, 9]) *B*2
3
1
9
1*b*
``` 在 2.0 及以后版本, ``shuffle`` 函数可以用于打乱一个列表的内容
(也就是生成一个该列表的随机全排列). [Example 2-31 #eg-2-31] 展示了如何在旧版本中实现该函数. ====Example 2-31. 使用 random 模块打乱一副牌====[eg-2-31] ```
File: random-example-4.py import random try:
# available in 2.0 and later
shuffle = random.shuffle
except AttributeError:
def shuffle(x):
for i in xrange(len(x)-1, 0, -1):
# pick an element in x[:i+1] with which to exchange x[i]
j = int(random.random() * (i+1))
x[i], x[j] = x[j], x[i] cards = range(52) shuffle(cards) myhand = cards[:5] print myhand *B*[4, 8, 40, 12, 30]*b*
``` ``random`` 模块也包含了非恒定分布的随机生成器函数. [Example 2-32 #eg-2-32]
使用了 gauss (高斯)函数来生成满足高斯分的布随机数字. ====Example 2-32. 使用 random 模块生成高斯分布随机数====[eg-2-32] ```
File: random-example-3.py import random histogram = [0] * 20 # calculate histogram for gaussian
# noise, using average=5, stddev=1
for i in range(1000):
i = int(random.gauss(5, 1) * 2)
histogram[i] = histogram[i] + 1 # print the histogram
m = max(histogram)
for v in histogram:
print "*" * (v * 50 / m) *B*****
**********
*************************
***********************************
************************************************
**************************************************
*************************************
***************************
*************
***
**b*
``` 你可以在 //Python Library Reference// 找到更多关于非恒定分布随机生成器函数的信息. *Note*标准库中提供的随机数生成器都是伪随机数生成器. 不过这对于很多目的来说已经足够了, 比如模拟, 数值分析, 以及游戏. 可以确定的是它不适合密码学用途.*note*

python标准库介绍——27 random 模块详解的更多相关文章

  1. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  2. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  3. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  4. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  5. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  6. python标准库介绍——33 thread 模块详解

    ?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...

  7. python标准库介绍——32 Queue 模块详解

    Queue 模块 ``Queue`` 模块提供了一个线程安全的队列 (queue) 实现, 如 [Example 3-2 #eg-3-2] 所示. 你可以通过它在多个线程里安全访问同个对象. ==== ...

  8. python标准库介绍——31 threading 模块详解

    threading 模块 (可选) ``threading`` 模块为线程提供了一个高级接口, 如 [Example 3-1 #eg-3-1] 所示. 它源自 Java 的线程实现. 和低级的 ``t ...

  9. python标准库介绍——28 md5 模块详解

    ==md5 模块== ``md5`` (Message-Digest Algorithm 5)模块用于计算信息密文(信息摘要). ``md5`` 算法计算一个强壮的128位密文. 这意味着如果两个字符 ...

随机推荐

  1. (转)深入浅出K-Means算法

    原文地址:http://www.csdn.net/article/2012-07-03/2807073-k-means 摘要:在数据挖掘中,K-Means算法是一种 cluster analysis ...

  2. 彻底解决 intellij IDEA 卡顿 优化笔记

    由于工作中经常出现分支各种切换,使用Eclipse便不再像以前那么舒服了,不停的修改工作空间,每次修改完工作空间又是一堆一堆的个性化设置,来回的切换,真的很累.我们做软件的,怎么能不去尝试新鲜的呢,毕 ...

  3. GG配置ggmgr进程

    Oracle配置mgr进程 edit params mgr port 7809 syslog none dynamicportlist 7810-7820 自己主动会生成./dirprm/mgr.pr ...

  4. Jmeter-Maven-Plugin高级应用:Remote Server Configuration

    Remote Server Configuration Pages 12 Home Adding additional libraries to the classpath Advanced Conf ...

  5. 在Win7上安装MySql5.2遇到Write configuration file的解决

    机器从XP翻新到Win7后,原有的环境也被清除了,因此找了个时间重新安装MySql. 以前轻车熟路的过程,在最后一环却卡住了,出现Write configuration file 错误. 以前从来没有 ...

  6. 关于oracle redo log buffer 你所不知道的东西

    [ora11@lixora ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Wed Oct 8 09:57:50 ...

  7. 禁止CloudStack删除Xenserver原有虚拟机

    CloudStack在文档中指明需要加入一台干净的Xenserver作为hyperviser. 但是实际使用中,总会存在不同的需求,很多场景是试用CloudStack接管当前已有的hyperviser ...

  8. tail 命令(转)

    原文:http://www.cnblogs.com/peida/archive/2012/11/07/2758084.html ail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可 ...

  9. 安装ubuntu后不能从ubuntu引导修复方法

    sudo fdisk -l sudo -i mkdir /media/tempdir mount /dev/sda7 /media/tempdir grub-install --root-direct ...

  10. SQLite的升级(转)

    做Android应用,不可避免的会与SQLite打交道.随着应用的不断升级,原有的数据库结构可能已经不再适应新的功能,这时候,就需要对SQLite数据库的结构进行升级了. SQLite提供了ALTER ...