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 系列的资源整理,由 ...
随机推荐
- GZipStream 压缩与解压数据
简介:此类表示 GZip 数据格式,它使用无损压缩和解压缩文件的行业标准算法.这种格式包括一个检测数据损坏的循环冗余校验值.GZip 数据格式使用的算法与 DeflateStream 类的算法相同,但 ...
- python--使用队列结构来模拟共享打印机等候时间
按书里的样例抄的. 可以看到,将打印速度由第分钟5页提高到10页之后, 每个学生提交打印任务到打印完成的时间明显缩短. =========================== 在计算机科学实验室里考虑 ...
- springboot学习(一):创建项目
package com.glory.demo.Controller; import org.springframework.stereotype.Controller; import org.spri ...
- crunch创建自己的密码字典文件
http://www.2cto.com/article/201608/542026.html
- mysql配置完半同步复制之后报错[ERROR] The server quit without updating PID file
修改配置,MySQL启动报:[ERROR] The server quit without updating PID file [root@localhost mysql]# /etc/init.d/ ...
- net core体系-web应用程序-4net core2.0大白话带你入门-6asp.net core配置文件
asp.net core配置文件 读取配置文件 asp.net core使用appsettings.json代替传统.net framework的web.config中的<appSettin ...
- php通过CURL模拟post提交请求
<?php header("Content-type:text/html;charset=utf-8"); class Test{ public function reque ...
- thinkphp 3.1.3 配置debug开启报错
今天配置了一下thinkphp3.1.3 报错 无法加载模块:index 错误位置 FILE: D:\phpStudy\WWW\wwqq\thinkphp\Common\functions.php L ...
- Codeforces 109D String Transformation 字符串 哈希 KMP
原文链接https://www.cnblogs.com/zhouzhendong/p/CF109D.html 题目传送门 - CF109D 题意 给定两个字符串 $a,b$ ,求一组 $i,j$ 使得 ...
- multiprocssing,threading,多进程多线程初识
1. multiprocessing 多进程: import multiprocessing p = multiprocessing.Process(target = func,name = &quo ...