python常用库之random
random是内建(built-in)函数,作用是产生随机数,使用较多.
1.导入模块
首先是导入模块:
import random
接着就可以调用random模块下的函数了,例如:
random.randint(1,100)
语句的含义是产生1至100(包含1与100)的一个随机数。
也可以: from random import randint #直接导入randint函数
还可以改名呢: from random import randint as ra_int
就可以直接调用ra_int函数了,适用于名字较长的函数,写代码时方便。
2.random模块下函数
使用 dir(random)可以查看random模块下有哪些函数,结果如下:
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hashlib', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
3.最常用的函数有如下:
(1)生成一个0到1之间的随机浮点数,包括0但不包括1,也就是[0.0, 1.0)。
random.random()
(2)生成a、b之间的随机浮点数。不过与randint不同的是,a、b无需是整数,也不用考虑大小。
random.uniform(a, b)
(3)从序列中随机选取一个元素。seq需要是一个序列,比如list、元组、字符串。
random.choice(seq)
例:
random.choice([1, 2, 3, 5, 8, 13]) #list
random.choice('hello') #字符串
random.choice(['hello', 'world']) #字符串组成的list
random.choice((1, 2, 3)) #元组
都是可行的用法。
(4)生成一个从start到stop(不包括stop),间隔为step的一个随机数。start、stop、step都要为整数,且start<stop。
random.randrange(start, stop, step)
(5)从population序列中,随机获取k个元素,生成一个新序列。sample不改变原来序列。
random.sample(population, k)
(6)把序列x中的元素顺序打乱。shuffle直接改变原有的序列。
random.shuffle(x)
(7) seed()方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数, 注意其实是伪随机数,只要初始值一样,得到的结果会是一样的,在python中,默认用系统时间作为seed。你也可以手动调用random.seed(x)来指定seed。
python常用库之random的更多相关文章
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
- python 常用库整理
python 常用库整理 GUI 图形界面 Tkinter: Tkinter wxPython:wxPython pyGTK:PyGTK pyQt:pyQt WEB框架 django:django w ...
- python常用库安装网址
python常用库安装网址如下: http://pypi.python.org/pypi
- python常用模块之random模块
python常用模块之random模块 在程序中很多会用到随机字符,比如登陆网站的随机验证码,通过random模块可以很容易生成随机字符串 1.random.randrange():返回1-10之间的 ...
- windows下python常用库的安装
windows下python常用库的安装,前提安装了annaconda 的python开发环境.只要已经安装了anaconda,要安装别的库就很简单了.只要使用pip即可,正常安装好python,都会 ...
- Python常用库整理
Python常用库整理 Python中到底有哪些库会让程序员爱不释手?以至于一次上瘾,造成永久性伤害(这句话好像在哪里见过),今天我们就来整理一番这样的库,欢迎各位在评论区或者私信我添加或者修改相关库 ...
- python标准库总的random函数用法
Python标准库中的random函数,可以生成随机浮点数.整数.字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等.random中的一些重要函数的用法:1 ).random() 返回0& ...
- python常用库(转)
转自http://www.west999.com/info/html/wangluobiancheng/qita/20180729/4410114.html Python常用的库简单介绍一下 fuzz ...
- python常用库
本文由 伯乐在线 - 艾凌风 翻译,Namco 校稿.未经许可,禁止转载!英文出处:vinta.欢迎加入翻译组. Awesome Python ,这又是一个 Awesome XXX 系列的资源整理,由 ...
随机推荐
- ipmitool获取服务器信息
https://www.annhe.net/article-2987.html https://blog.csdn.net/wolovexiexiongfei/article/details/8270 ...
- Eclipse+Maven整合开发Java项目(二)➣webapp3.0以上的Maven项目
概述 Eclipse集成Maven插件,新建maven-archetype-webapp项目的时候,采用的webapp的版本较低,默认是2.3,有些时候,我们希望升级Webapp的版本到3.0(Tom ...
- mongodb实现自增的方法
前面操作看菜鸟教程 function getNextSequenceValue(sequenceName){ var sequenceDocument = Counter.findOneAndUpda ...
- Adjoint operators $T_K$ and $T_{K^{*}}$ in BEM
In our last article, we introduced four integral operators in the boundary integral equations in BEM ...
- spark操作kudu之DML操作
Kudu支持许多DML类型的操作,其中一些操作包含在Spark on Kudu集成 包括: INSERT - 将DataFrame的行插入Kudu表.请注意,虽然API完全支持INSERT,但不鼓励在 ...
- net core体系-web应用程序-4net core2.0大白话带你入门-6asp.net core配置文件
asp.net core配置文件 读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettin ...
- 高性能之js
alloyteam团队的总结: 链接在:http://www.alloyteam.com/2012/10/high-performance-front-end-high-performance-jav ...
- Nginx反向代理的基本配置
(1)proxy_pass 语法:proxy_pass URL; 配置块:location.if 此配置项将当前请求反向代理到URL参数指定的服务器上,URL可以是主机名或IP地址加端口的形式,例如: ...
- BZOJ1458 士兵占领 网络流 最大流 SAP
原文链接http://www.cnblogs.com/zhouzhendong/p/8384699.html 题目传送门 - BZOJ1458 题意概括 有一个M * N的棋盘,有的格子是障碍.现在你 ...
- Period kmp
For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...