*#【Python】【基础知识】【模块】【random】【使用random创造一个随机数】
Random介绍:
输出随机数。
快照:
#!/usr/bin/python
# -*- coding: UTF-8 -*- import random #生成 10 到 20 之间的随机数
print(random.uniform(10, 20)) #输出1-20间的随机数(包含整数、小数)
print(random.random()) #输出0-1间的随机数
print(random.randint(10, 20)) #输出10-20间的随机整数
print(random.choice([10,11,12,13,14,15])) #随机输出序列内的一个元素
print(random.choice([x for x in range(1,100)])) #输出1-99间的随机数
random模块的方法有:
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_os', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
uniform 方法的使用:
>>> dir(random.uniform)
['__call__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__func__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> help(random.uniform)
Help on method uniform in module random: uniform(a, b) method of random.Random instance
Get a random number in the range [a, b) or [a, b] depending on rounding.
翻译:关于random模块的uniform方法的一些帮助;
random的uniform(a,b) 方法。
random例子:
在[a,b) 或[a,b] 范围内,根据四舍五入,获取一个随机数。
原题:
使用random创建一个随机数;
原题给出的解答:
#!/usr/bin/python
# -*- coding: UTF-8 -*- import random #生成 10 到 20 之间的随机数
print(random.uniform(10, 20))
输出的结果:

结果精确到小数点后14位;
choice方法的使用
random.choice(seq)
其参数为序列,从序列中随机获取一个随机元素;
示例:
>>> random.choice([1,2,3,4,5,6,7,3])
2
>>> random.choice([1,2,3,4,5,6,7,3])
2
>>> random.choice([1,2,3,4,5,6,7,3])
5
>>> random.choice([1,2,3,4,5,6,7,3])
4
>>> random.choice([1,2,3,4,5,6,7,3])
3
>>> random.choice([1,2,3,4,5,6,7,3])
4
>>> random.choice([1,2,3,4,5,6,7,3])
6
>>> random.choice([1,2,3,4,5,6,7,3])
6
>>> random.choice([1,2,3,4,5,6,7,3])
3
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice([1,2,3,4,5,6,7,3])
5
>>> random.choice([1,2,3,4,5,6,7,3])
6
>>> random.choice([1,2,3,4,5,6,7,3])
3
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice([1,2,3,4,5,6,7,3])
3
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice([1,2,3,4,5,6,7,3])
5
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice("test")
't'
>>> random.choice("test")
't'
>>> random.choice("test")
't'
>>> random.choice("test")
'e'
>>> random.choice("test")
't'
>>> random.choice("test")
's'
>>> random.choice("test")
'e'
————————(我是分割线)————————
参考:
1. RUNOOB.COM:https://www.runoob.com/python/python-exercise-example50.html
2.Python DOC
备注:
初次编辑时间:2019年10月7日16:12:06
环境:Windows 7 / Python 3.7.2
*#【Python】【基础知识】【模块】【random】【使用random创造一个随机数】的更多相关文章
- Python 基础之模块之math random time
一:math 数学模块import math#(1)ceil() 向上取整操作 (对比内置round)res = math.ceil(6.001) #注意精度损耗print(res)#(2)floo ...
- 1.7 Python基础知识 - 模块初识
在Python中有很多模块,模块对应的就是python源代码文件.模块中有Python程序自己附带的标准模块,还有很多其他人共享的第三方模块.模块中可以定义变量.函数和类.而多个功能类似的模块可以组织 ...
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...
- python基础知识小结-运维笔记
接触python已有一段时间了,下面针对python基础知识的使用做一完整梳理:1)避免‘\n’等特殊字符的两种方式: a)利用转义字符‘\’ b)利用原始字符‘r’ print r'c:\now' ...
- python基础知识的学习和理解
参考链接:https://github.com/yanhualei/about_python/tree/master/python_learning/python_base python基础知识笔 ...
- python 基础知识(一)
python 基础知识(一) 一.python发展介绍 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本 ...
- 二十五. Python基础(25)--模块和包
二十五. Python基础(25)--模块和包 ● 知识框架 ● 模块的属性__name__ # my_module.py def fun1(): print("Hello& ...
- Python 基础知识(一)
1.Python简介 1.1.Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时 ...
- Python基础知识(六)------小数据池,集合,深浅拷贝
Python基础知识(六)------小数据池,集合,深浅拷贝 一丶小数据池 什么是小数据池: 小数据池就是python中一种提高效率的方式,固定数据类型使用同一个内存地址 代码块 : 一个文 ...
- Python基础知识(五)------字典
Python基础知识(四)------字典 字典 一丶什么是字典 dict关键字 , 以 {} 表示, 以key:value形式保存数据 ,每个逗号分隔 键: 必须是可哈希,(不可变的数据类型 ...
随机推荐
- pyecharts v1 版本 学习笔记 饼图,玫瑰图
饼图: 普通案例 from example.commons import Faker from pyecharts import options as opts from pyecharts.char ...
- 【AGC030F】Permutation and Minimum(DP)
题目链接 题解 首先可以想到分组后,去掉两边都填了数的组. 然后就会剩下\((-1,-1)\)和\((-1,x)\)或\((x,-1)\)这两种情况 因为是最小值序列的情况数,我们可以考虑从大到小填数 ...
- 【概率论】1-2:计数方法(Counting Methods)
title: [概率论]1-2:计数方法(Counting Methods) categories: Mathematic Probability keywords: Counting Methods ...
- 【线性代数】7-2:线性变化的矩阵(The Matrix of a Linear Transformation)
title: [线性代数]7-2:线性变化的矩阵(The Matrix of a Linear Transformation) categories: Mathematic Linear Algebr ...
- c++ 排序,<< 运算符重载
#include <iostream> #include <functional> #include <list> using namespace std; ost ...
- League of Leesins
C - League of Leesins 首先找到每一串数字的头和尾两个数字,这两个数字有一个特点,就是它们在输入数据的时候都只会出现一次.我们在输出的时候用头和尾做第一数都可以. 然后第二个数只会 ...
- JavaWeb_(Spring框架)xml配置文件
系列博文 JavaWeb_(Spring框架)xml配置文件 传送门 JavaWeb_(Spring框架)注解配置 传送门 Xml配置 a)Bean元素:交由Spring管理的对象都要配置在bean ...
- Hadoop 3.2.1 win10 64位系统 vs2015 编译
Hadoop 3.2.1 win10 64位系统 vs2015 编译 1 环境配置 1.1 JDK下载安装 1.1.1 下载 JDK 1.8 (jdk1.8.0 ...
- linux 后台 运行
但是如果终端关闭的话,程序也会终止,那么就要涉及到linux的一个十分强大的命令:screen. 按照我个人的理解,这个命令就是能够在linux中创造出多个终端,在已有的窗口内部再创造更多的窗口,结合 ...
- SSH交互式脚本StrictHostKeyChecking选项 benchmode=yes
SSH 公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击.但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖 SSH 协议的自动化任务,就需要一种手段能够绕过 SSH 的公钥检查. 什 ...