*#【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形式保存数据 ,每个逗号分隔 键: 必须是可哈希,(不可变的数据类型 ...
随机推荐
- I have Flash Player installed, but I am unable to view Flash content in Chromium. How do I enable Flash Player to view this content?
I have Flash Player installed, but I am unable to view Flash content in Chromium. How do I enable Fl ...
- iis大文件上传
IS出于安全考虑限制了大文件的上传,而网上百度到的大部分解决方法都是用一个管理员权限的记事本打开一个文件修改参数,但是我发现里面根本没有网上所说的那些参数,最后自己找到了修改发布文件的webconfi ...
- [Luogu] 家族
https://www.luogu.org/problemnew/show/1767 字符串的读入有点麻烦 #include <cstdio> #include <cstring&g ...
- Hihocoder #1333 : 平衡树·Splay2
1333 : 平衡树·Splay2 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:好麻烦啊~ 小Hi:小Ho你在干嘛呢? 小Ho:我在干活啊!前几天老师让我帮忙 ...
- qt5.6.1 +vs2015 自定义控件 不在designer显示
qt designer 不显示自定义插件, qt5.6.1下在bin下点击designer.exe 打开qdesigner 点击帮助- 关于插件. 显示: 在vs2015 窗口中点击qt vs too ...
- 泰乐事(Telos)白皮书中文版 <一> 泰乐事项目(未完成)
泰乐事项目 一个可持续发展的去中心化EOSIO网络 EOS系统在设计上为人们带来了令人惊喜的一种实现新经济模式的承诺.然而很不幸,其高度中心化的运营正不断地破坏这种承诺.90%的EOS代币掌握在仅仅1 ...
- libpng warning:iCCP:known incorrect sRGB profile
原因是新版的libpng增强了检查,发出警告.此警告可以忽略.若要消除此警告则要使用v4的色彩配置.GIMP sRGB v4 色彩配置,修改当前图片的色彩配置,设为默认. sRGB profilesO ...
- codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)
题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...
- spring boot + swagger2
spring boot集成swagger2: swagger2是一个基于restful的开源设计,构建,文档,访问的开源工具集.开发中它的在线可视化文档功能,可以动态生成文档,简化前后对接工作 ...
- 从源码看Java集合之ArrayList
Java集合之ArrayList - 吃透增删查改 从源码看初始化以及增删查改,学习ArrayList. 先来看下ArrayList定义的几个属性: private static final int ...