模块源码:

Source code: Lib/random.py

文档:http://docs.python.org/2/library/random.html

常用方法:

random.random()

Return the next random floating point number in the range [0.0, 1.0).

random.randint(ab)包括b

Return a random integer N such that a <= N <= b.

random.uniform(ab)

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.

The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) *random().

用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: b <= n <= a。如果 a <b, 则 a <= n <= b。

print random.uniform(10, 20)
print random.uniform(20, 10)
#---- 结果(不同机器上的结果不一样)
#18.7356606526
#12.5798298022

random.choice(seq)

Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

这个写代码可能会用到。

random.randrange(stoprandom.randrange(startstop[, step])

Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.

记住常用的就可以了。

python模块学习之random的更多相关文章

  1. Python模块学习笔记— —random

    Python中的random模块用于生成随机数. random.random 函数原型 random.random() 生成一个范围在[0,1)的随机浮点数. import random print ...

  2. 【转】Python模块学习 - fnmatch & glob

    [转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...

  3. 【目录】Python模块学习系列

    目录:Python模块学习笔记 1.Python模块学习 - Paramiko  - 主机管理 2.Python模块学习 - Fileinput - 读取文件 3.Python模块学习 - Confi ...

  4. Python模块学习filecmp文件比较

    Python模块学习filecmp文件比较 filecmp模块用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单.python标准库还提供了difflib模块用于比较文件的内容.关于dif ...

  5. python模块学习第 0000 题

    将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果: 好可爱>%<! 题目来源:https://github.com/Yixiao ...

  6. Python模块学习:logging 日志记录

    原文出处: DarkBull    许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net ...

  7. python模块详解 random os

    random模块 常用方法 random.random() 随机产生一个小于1的浮点数 import random print(random.random()) #0.4153761818276826 ...

  8. 解惑Python模块学习,该如何着手操作...

    Python模块 晚上和朋友聊天,说到公司要求精兵计划,全员都要有编程能力.然后C.Java.Python-对于零基础入门的,当然是选择Python的人较多了.可朋友说他只是看了简单的语法,可pyth ...

  9. Python模块学习

    6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...

随机推荐

  1. WPF Image触摸移动方法

    1: TouchPoint mPoint = null; 2: double mOffsetX;//水平滚动条当前位置 3: double mOffsetY;//垂直滚动条当前位置 4: bool m ...

  2. Python与MySQL首次交互

    前两天在工作之余研究了一下Python,对基础有了大致了解,就想拿她很MqSQL交互一下. 一开始就遇到了问题,要import MySQLdb,search发现有人说安装mysql-python,于是 ...

  3. poj 2305(指定进制,大数取模)

    题意:输入一个进制b,在输入两个基于b进制的大整数 x,y ,求x%y的b进制结果. http://162.105.81.212/JudgeOnline/problem?id=2305 函数: Str ...

  4. mysql基本介绍

    RDBMS:1.数据库创建.删除除2.创建表.删除表.修改表3.索引的创建.删除4.用户和权限5.数据增.删.改6.查询 DML:Data Manapulate Language: 数据操作语言   ...

  5. 如何在自定义Listener(监听器)中使用Spring容器管理的bean

    正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...

  6. IOS开发之语音合成(科大讯飞)详解

    1.注册讯飞账号,申请APPID(注意选择IOS平台) 2.加载所需要的类库 3.导入所需要的类库文件头 4.调用申请的APPID以及所需函数,完成语音合成(需要参考官方给出的SDK文件)   详细步 ...

  7. servlet基本概念

    一.servlet是一个供其它java程序调用的java类,比方tomcatserver,它不能独自执行,它的执行由servlet引擎来控制和调度. 二.servlet是单例,多线程 针对多个clie ...

  8. vim的正则表达式(二)应用实例

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  9. [Regex Expression] Confirmative -- World bundry

    String to check: As it turns out, our potential shipmates are extremely superstitious. As such, we d ...

  10. css-下拉菜单案例

    <!DOCTYPE html>CSS4-布局2-display下拉菜单案例 <style>.xiala{width:200px;background:#ddd;}.xiala ...