python中random.randint和random.randrange的区别详解


Posted in Python onSeptember 20, 2020

在python中,通过导入random库,就能使用randint 和 randrange
这两个方法来产生随机整数。那这两个方法的区别在于什么地方呢?让我们一起来看看!

区别:

randint 产生的随机数区间是包含左右极限的,也就是说左右都是闭区间的[1, n],能取到1和n。而 randrange 产生的随机数区间只包含左极限,也就是左闭右开的[1, n),1能取到,而n取不到。randint 产生的随机数是在指定的某个区间内的一个值,而 randrange 产生的随机数可以设定一个步长,也就是一个间隔。

区别1——例子:

randint 示例:

import random
while True:
 rdt = random.randint(1, 3)
 print(rdt, end="")
 if rdt == 3:
  break

输出结果:产生随机数3即停止。

C:\Users\ASUS\AppData\Local\Programs\Python\Python37\python.exe F:/python/test.py
1121213
Process finished with exit code 0

randrange 示例:

import random
while True:
 rdt = random.randrange(1, 3)
 print(rdt, end="")
 if rdt == 3:
  break

输出结果:永远无法取到有边界,无线循环下去。

python中random.randint和random.randrange的区别详解

区别2——例子:

randint 示例:

import random
for i in range(1, 10):
 print(random.randint(1, 100, 10), end=" ")

输出结果:randint 无法设定步长,报如下错误!

python中random.randint和random.randrange的区别详解

randrange 示例:

import random
for i in range(1, 10):
 print(random.randrange(1, 100, 10), end=" ")

输出结果:设定步长为10,产生的随机数间隔都是10的整数倍。

C:\Users\ASUS\AppData\Local\Programs\Python\Python37\python.exe F:/python/test.py
31 31 71 21 21 31 41 71 31
Process finished with exit code 0

到此这篇关于python中random.randint和random.randrange的区别详解的文章就介绍到这了,更多相关python random.randint random.randrange内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
在Python的Django框架中加载模版的方法
Jul 16 Python
Python中生成Epoch的方法
Apr 26 Python
Python错误: SyntaxError: Non-ASCII character解决办法
Jun 08 Python
Python实现的递归神经网络简单示例
Aug 11 Python
详解如何用OpenCV + Python 实现人脸识别
Oct 20 Python
python实现将excel文件转化成CSV格式
Mar 22 Python
详解Python装饰器
Mar 25 Python
python numpy--数组的组合和分割实例
Feb 24 Python
Python在字符串中处理html和xml的方法
Jul 31 Python
matplotlib事件处理基础(事件绑定、事件属性)
Feb 03 Python
利用python做表格数据处理
Apr 13 Python
Python排序算法之插入排序及其优化方案详解
Jun 11 Python
详解如何在pyqt中通过OpenCV实现对窗口的透视变换
Sep 20 #Python
Python Pillow(PIL)库的用法详解
Sep 19 #Python
Python自动化xpath实现自动抢票抢货
Sep 19 #Python
python 贪心算法的实现
Sep 18 #Python
详解KMP算法以及python如何实现
Sep 18 #Python
python实现二分查找算法
Sep 18 #Python
Python自定义sorted排序实现方法详解
Sep 18 #Python
You might like
PHP运行出现Notice : Use of undefined constant 的完美解决方案分享
2012/03/05 PHP
php实现屏蔽掉黑帽SEO的搜索关键字
2015/04/15 PHP
yii2项目实战之restful api授权验证详解
2017/05/20 PHP
PHP获取数组中单列值的方法
2017/06/10 PHP
Laravel框架实现即点即改功能的方法分析
2019/10/31 PHP
jQuery操作 input type=checkbox的实现代码
2012/06/14 Javascript
EasyUI 中 MenuButton 的使用方法
2012/07/14 Javascript
javascript通过className来获取元素的简单示例代码
2014/01/10 Javascript
javascript设计模式之解释器模式详解
2014/06/05 Javascript
jQuery简单实现列表隐藏和显示效果示例
2016/09/12 Javascript
BootStrap 动态添加验证项和取消验证项的实现方法
2016/09/28 Javascript
详解Javascript中的原型OOP
2016/10/12 Javascript
bootstrap datetimepicker2.3.11时间插件使用
2016/11/19 Javascript
微信小程序 页面跳转传递值几种方法详解
2017/01/12 Javascript
JavaScript实现form表单的多文件上传
2020/03/27 Javascript
vue 挂载路由到头部导航的方法
2017/11/13 Javascript
vue中promise的使用及异步请求数据的方法
2018/11/08 Javascript
微信小程序调用天气接口并且渲染在页面过程详解
2019/06/24 Javascript
[00:32]2018DOTA2亚洲邀请赛EG出场
2018/04/03 DOTA
[01:01:24]LGD vs Fnatic 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
[03:15]DOTA2-DPC中国联赛1月22日Recap集锦
2021/03/11 DOTA
python操作mongodb根据_id查询数据的实现方法
2015/05/20 Python
flask入门之文件上传与邮件发送示例
2018/07/18 Python
Python面向对象程序设计之私有属性及私有方法示例
2019/04/08 Python
Python获取当前脚本文件夹(Script)的绝对路径方法代码
2019/08/27 Python
python数据分析工具之 matplotlib详解
2020/04/09 Python
学会python自动收发邮件 代替你问候女友
2020/05/20 Python
tensorflow pb to tflite 精度下降详解
2020/05/25 Python
CSS3 @font-face属性使用指南
2014/12/12 HTML / CSS
洲际酒店集团大中华区:IHG中国
2016/08/17 全球购物
自我评价是什么
2014/01/04 职场文书
药剂专业求职信
2014/06/20 职场文书
北京英文导游词
2015/02/12 职场文书
2016年小学生清明节广播稿
2015/12/17 职场文书
《颐和园》教学反思
2016/02/19 职场文书
一次SQL如何查重及去重的实战记录
2022/03/13 MySQL