Python random模块random/uniform/randint/choice/getrandbits/shuffle/choice/sample随机函数
1.random.random()
返回0<=n<1之间的随机实数n
2. random.uniform()
弥补了上面函数的不足,它可以设定浮点数的范围,一个是上限,一个是下限。
3. random.randint()
随机生成一个整数int类型,可以指定这个整数的范围,同样有上限和下限值
4. random.choice(seq)
从任何序列,比如list列表中,选取一个随机的元素返回,可以用于字符串、列表、元组等
5. random.getrandbits(n)
以长整型形式返回n个随机位
6. random.shuffle(seq[, random])
将一个序列中的元素,随机打乱的话可以用这个函数方法
7. random.sample(seq, n)
从序列seq中选择n个随机且独立的元素。从指定的序列中,随机的截取指定长度的片段,不作原地修改。
举例:
from random import randint, sample
date = [randint(10,20) for _ in range(10)]
c = sample(date, 5)
print(c)
Python random模块random/uniform/randint/choice/getrandbits/shuffle/choice/sample随机函数的更多相关文章
- python shutil模块&random模块
shutil模块 import shutil shutil.copyfileobj(open("os_.py", "r"), open("os_2.p ...
- python 随机数模块 -- random
一.概述 这个模块实现的伪随机数生成器. 对于整数,从区间选取.对于序列,随机元素. 在实线的,有功能来计算均匀分布,正态分布(高斯) ,对数正态分布,负指数,γ和β分布.对于生成的角度分布,冯·米塞 ...
- Python:time模块/random模块/os模块/sys模块
time 模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 python中时间日期格式化符号: %y 两位数的 ...
- python 基本模块 random、os、sys
一.random模块 所有关于随机相关的内容都在random模块中 import random print(random.random()) # 0-1⼩数 print(random.uniform( ...
- python常用模块: random模块, time模块, sys模块, os模块, 序列化模块
一. random模块 import random # 任意小数 print(random.random()) # 0到1的任意小数 print(random.uniform(-10, 10)) # ...
- python常用模块——random模块
参考博客:http://www.360doc.com/content/14/0430/11/16044571_373443266.shtml 今天突然想起python该怎么生成随机数?查了一下,贴出实 ...
- python之模块random,time,os,sys,序列化模块(json,pickle),collection
引入:什么是模块: 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类型. 1.使用python编写的代码(.py ...
- python 之模块random
随机形成字母和数字组成的五位字符码. [root@localhost python]# vim timee.py import random def coder(): code = '' for i ...
- python 随机模块random
随机推荐
- nodejs----http系统模块使用和fs系统模块
----- const http=require("http"); const fs=require("fs"); var httpserver=http.cr ...
- 简单的Git流程
一.安装cygwin 略 二. 1.创建一个文件 $echo "中华人民共和国">>abc.txt 2.添加到文件追踪 $git add abc.txt 上一行是提交一 ...
- 关于Java程序流程控制的整理(已完善)
- echart四川地图
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- [daily] pandoc
学了LaTeX之后,你就会很自然的接触的另一种观点: LaTeX是专注于排版的,你需要专注于内容. 于是,请使用pandoc. 简单的说, pandoc就是可以将各种个样格式的文档转换成各种各样格式的 ...
- dp的斜率优化
对于刷题量我觉得肯定是刷的越多越好(当然这是对时间有很多的人来说. 但是在我看来我的确适合刷题较多的那一类人,应为我对知识的应用能力并不强.这两天学习的内容是dp的斜率优化.当然我是不太会的. 这个博 ...
- Delphi2010分 AnsiChar(1个字节) 和WideChar(2个字节) 。D7都是AnsiChar。
Delphi2010分 AnsiChar(1个字节) 和WideChar(2个字节) .D7都是AnsiChar.
- JTAG 工作原理
- mvc,EntityFramework调用分页存储过程
此文讲述mvc4+entityframework6+sqlserver2008环境下调用存储过程,实现分页. 1.分页存储过程代码如下: 分页原理用的row_number()和over()函数实现(没 ...
- spring mvc 资源映射配置
在springmvc配置文件中添加 <mvc:resources location="/css/" mapping="/css/**"/> < ...