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创造一个随机数】的更多相关文章

  1. Python 基础之模块之math random time

    一:math 数学模块import math#(1)ceil() 向上取整操作 (对比内置round)res = math.ceil(6.001)  #注意精度损耗print(res)#(2)floo ...

  2. 1.7 Python基础知识 - 模块初识

    在Python中有很多模块,模块对应的就是python源代码文件.模块中有Python程序自己附带的标准模块,还有很多其他人共享的第三方模块.模块中可以定义变量.函数和类.而多个功能类似的模块可以组织 ...

  3. python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。

    本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...

  4. python基础知识小结-运维笔记

    接触python已有一段时间了,下面针对python基础知识的使用做一完整梳理:1)避免‘\n’等特殊字符的两种方式: a)利用转义字符‘\’ b)利用原始字符‘r’ print r'c:\now' ...

  5. python基础知识的学习和理解

    参考链接:https://github.com/yanhualei/about_python/tree/master/python_learning/python_base   python基础知识笔 ...

  6. python 基础知识(一)

    python 基础知识(一) 一.python发展介绍 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本 ...

  7. 二十五. Python基础(25)--模块和包

    二十五. Python基础(25)--模块和包 ● 知识框架   ● 模块的属性__name__ # my_module.py   def fun1():     print("Hello& ...

  8. Python 基础知识(一)

    1.Python简介 1.1.Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时 ...

  9. Python基础知识(六)------小数据池,集合,深浅拷贝

    Python基础知识(六)------小数据池,集合,深浅拷贝 一丶小数据池 什么是小数据池: ​ 小数据池就是python中一种提高效率的方式,固定数据类型使用同一个内存地址 代码块 : ​ 一个文 ...

  10. Python基础知识(五)------字典

    Python基础知识(四)------字典 字典 一丶什么是字典 ​ dict关键字 , 以 {} 表示, 以key:value形式保存数据 ,每个逗号分隔 ​ 键: 必须是可哈希,(不可变的数据类型 ...

随机推荐

  1. 03_每周 5 使用 tar 命令备份/var/log 下的所有日志文件

    ]# vim /root/logbak.shtar -czf log-`date +%Y%m%d`.tar.gz /var/log ]# crontab -e -u root00 03 * * 5 / ...

  2. Codeforces 1220 E Tourism

    题面 可以发现一个边双必然是可以随意走的,所以我们就把原图求割边然后把边双缩成一个点,然后就是一个树上dp了. #include<bits/stdc++.h> #define ll lon ...

  3. 关于要python爬虫你需要的一些网址和信息[待补充]

    phantomjs无头浏览器(基本不用) http://phantomjs.org/download.html 如果报 下面这种错误 [root@hwgz01 ~]# phantomjs phanto ...

  4. iOS Jenkins 自动化打包构建

    前言 在测试app项目过程中,通常都是需要开发打测试包给到测试,但是无论是iOS还是Android的打包过程都是相当漫长的,频繁的回归测试需要频繁的打包,对于开发同学影响还是蛮大的.因此在这种情况下, ...

  5. Rand工具类

    这篇文章已经废弃. 实际开发中,这个工具类用到得非常少. RandN是主要类,用于生成指定位数的随机字符串,具体功能在这个类中实现 Rand8是修饰了RandN中每个对外方法的修饰类,用与生成8位的随 ...

  6. Spring Cloud Eureka(七):DiscoveryClient 源码分析

    1.本节概要 上一节文章主要介绍了Eureka Client 的服务注册的流程,没有对服务治理进行介绍,本文目的就是从源码角度来学习服务实例的治理机制,主要包括以下内容: 服务注册(register) ...

  7. AGC024E Sequence Growing Hard

    题意 给出\(n\),\(m\),\(mu\),问有多少个序列组\((A_0,A_1,\dots,A_n)\)满足: 序列\(Ai\)的长度恰好为\(i\) 所有元素均在\([1,m]\) \(A_{ ...

  8. 'builtin_function_or_method' object has no attribute 'translate'

    'builtin_function_or_method' object has no attribute 'translate' 首先这个错误的意思是:内建函数或方法对象不能转换成对应的属性. #错误 ...

  9. kafka - Confluent.Kafka

    上个章节我们讲了kafka的环境安装(这里),现在主要来了解下Kafka使用,基于.net实现kafka的消息队列应用,本文用的是Confluent.Kafka,版本0.11.6 1.安装: 在NuG ...

  10. aop 通知的执行顺序

    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(LoggerAop.class); /** * 线程池 异 ...