介绍 (Introduction)

In this tutorial, we are going to focus on the randint() method in Python. In our previous tutorials, we saw different random number generating methods defined inside the random module in our Random Number Tutorial in Python.

在本教程中,我们将重点介绍Python中的randint()方法。 在之前的教程中,我们在Python的《 随机数教程》中看到了在随机模块内部定义的不同随机数生成方法。

So, as you already know, we need to import the random module in Python first to begin using the randint() method. The module essentially creates pseudo-randomness.

因此,您已经知道,我们需要先在Python中导入random模块 ,才能开始使用randint()方法。 该模块实质上创建了伪随机性

randint()方法语法 (The randint() method Syntax)

Basically, the randint() method in Python returns a random integer value between the two lower and higher limits (including both limits) provided as two parameters.

基本上,Python中的randint()方法返回作为两个参数提供的两个较低较高限制(包括两个限制)之间的随机整数值。

It should be noted that this method is only capable of generating integer-type random value. Take a look at the syntax so that we can further incorporate the method.

应当注意,该方法仅能够生成整数型随机值。 看一下语法,以便我们可以进一步合并该方法。


#randint() Syntax
randint(lower limit , upper limit)

Here,

这里,

  • lower limit is the starting point from and including which the random integer would be generated,下限是生成随机整数的起点(包括该起点),
  • uppwer limit is the stopping point up to which the method would return the random integer.uppwer limit是该方法将返回随机整数的停止点。

The above example returns an integer N where N>=beg and N<=end.

上面的示例返回整数N ,其中N> = begN <= end。

It works in the same way randrange(beg,end) does, and hence is an alias for the same.

它与randrange(beg,end)工作方式相同,因此是相同的别名。

randint()方法示例 (The randint() Method Example)

Let us look at the given code below, it illustrates the use and working of the randint() method.

让我们看下面的给定代码,它说明randint()方法的使用和工作。


import random
beg=10
end=100
random_integer = random.randint(beg, end)
print("The random integer is :", random_integer)

Output:

输出:

randint() Example
randint()示例

Clearly, we can see that the randint() method generates a random integer value within the limit 1-100.

显然,我们可以看到randint()方法生成一个在1-100范围内的随机整数值。

Is this value random? What happens when we call the method multiple times? Does it return the same value?

这个值是随机的吗? 当我们多次调用该方法时会发生什么? 它返回相同的值吗?

多个randint()方法调用 (Multiple randint() Method Call)

The code snippet below answers all the above-mentioned questions and gives us a clear understanding.

下面的代码片段回答了所有上述问题,使我们有了清晰的认识。


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

Output:

输出:

Multiple Randint() Output
多个Randint()输出

For the above code, repeating the random.randint() method gives us different random integers for each call within the limit 10 to 100.

对于上面的代码,重复执行random.randint()方法,可为我们在10到100范围内的每个调用提供不同的随机整数。

Hence, we can infer that the values are random for each call and do not overlap in our case. Furthermore, when the number of calls is large and the range is quite smaller, in that case, the random values generated may collide or overlap.

因此,我们可以推断出每个调用的值都是随机的,在我们的情况下不重叠。 此外,当呼叫数量大且范围很小时,在这种情况下,生成的随机值可能会冲突重叠

As said earlier, one must ensure that the higher and lower limit parameters have to be an integer type. For other types, we get a ValueError as shown below.

如前所述,必须确保上限和下限参数必须是整数类型 。 对于其他类型,我们得到一个ValueError,如下所示。


import random
beg=5.3
end=10.2
print(random.randint(beg, end))

Output:

输出


Traceback (most recent call last):File "C:/Users/sneha/Desktop/test.py", line 4, in <module>print(random.randint(beg, end))File "C:\Users\sneha\AppData\Local\Programs\Python\Python37-32\lib\random.py", line 222, in randintreturn self.randrange(a, b+1)File "C:\Users\sneha\AppData\Local\Programs\Python\Python37-32\lib\random.py", line 186, in randrangeraise ValueError("non-integer arg 1 for randrange()")
ValueError: non-integer arg 1 for randrange()Process finished with exit code 1

结论 (Conclusion)

I hope this brief tutorial on the randint() method in Python has made the function clear for you. Your feedback is always welcome through the comments.

我希望这个关于Python的randint()方法的简短教程对您很清楚。 通过评论始终欢迎您的反馈。

参考资料 (References)

  • https://docs.python.org/3/library/random.htmlhttps://docs.python.org/3/library/random.html
  • https://www.journaldev.com/16134/python-random-numberhttps://www.journaldev.com/16134/python-random-number
  • https://stackoverflow.com/questions/34865409/python-and-random-randinthttps://stackoverflow.com/questions/34865409/python-and-random-randint

翻译自: https://www.journaldev.com/36085/randint-method-in-python

Python中的randint()方法相关推荐

  1. python里randint是什么意思_Python中random.randint方法(精选)

    Python中 random.randint方法的具体用法?Python 中random.randint怎么用?Python 中random.randint使用的例子?以下例子或许可以帮助到你: 示例 ...

  2. python计时器timeit返回秒数_一日一技:Python中的timeit()方法

    timeit()方法 python中的timeit()方法, 它用于获取代码的执行时间.该库将代码语句运行一百万次,并提供从集合中花费的最短时间.这是一种有用的方法,有助于检查代码的性能. 语法如下: ...

  3. python中timeit函数_一日一技:Python中的timeit方法

    timeit()方法 python中的timeit()方法, 它用于获取代码的执行时间.该库将代码语句运行一百万次,并提供从集合中花费的最短时间.这是一种有用的方法,有助于检查代码的性能. 语法如下: ...

  4. python中函数和方法的区别?Python编程判断当前获取的对象是函数还是方法

    python中函数和方法的区别?Python编程判断当前获取的对象是函数还是方法 目录

  5. python使用方法-在Python中使用next()方法操作文件的教程

    next()方法当一个文件被用作迭代器,典型例子是在一个循环中被使用,next()方法被反复调用.此方法返回下一个输入行,或引发StopIteration异常EOF时被命中. 与其它文件的方法,如Re ...

  6. python中range 10 0_如何在python中使用range方法

    如何在python中使用range方法 发布时间:2021-01-05 16:55:23 来源:亿速云 阅读:94 作者:Leah 如何在python中使用range方法?很多新手对此不是很清楚,为了 ...

  7. Python中sys.argv方法的一些典型用法

    本文整理汇总了Python中sys.argv方法的典型用法代码示例. 示例1: weather_icons # 需要导入模块: import sys [as 别名] # 或者: from sys im ...

  8. python中函数和方法的区别

    本篇内容主要介绍从几个维度来介绍下python中函数和方法的区别: 首先,从分类的角度来分析. (1)函数的分类: 内置函数:python内嵌的一些函数. 匿名函数:一行代码实现一个函数功能. 递归函 ...

  9. python脚本怎么使用_在Python中使用next()方法操作文件的教程

    next()方法当一个文件被用作迭代器,典型例子是在一个循环中被使用,next()方法被反复调用.此方法返回下一个输入行,或引发StopIteration异常EOF时被命中. 与其它文件的方法,如Re ...

最新文章

  1. IDEA报错总结:修改Java编译版本--maven项目
  2. 谷歌翻译无法连接网络_Windows无法连接网络,这几招教你解决
  3. ubuntu root下的无密码登陆
  4. 我教你怎么玩转git
  5. mariadb mysql版本对应_弹指神通MariaDB——MariaDB与MySQL各版本的区别
  6. 【.net 深呼吸】自定义应用程序配置节
  7. 4.2.1 OS之磁盘的结构(磁盘、磁道、扇区、盘面、柱面、磁头)
  8. 【干货】单日10亿GMV的.NET5电商平台,是如何设计的?
  9. 英语人机考试计算机算分吗,英语人机对话考试技巧
  10. git安装 tor_Tortoisegit图文使用教程
  11. Python——元组Tuple
  12. Go语言:基础数据类型
  13. centos mpeg acc 解码器安装
  14. protues 选项卡说明
  15. H5活动页面抽奖源码
  16. 数据结构导论 笔记整理
  17. UniApp引入极光推送
  18. hive 学习系列三(表格的创建create-table)
  19. echarts配合google地图,并自定义google地图的样式
  20. 向量点乘与差乘的区别,以及python下np.dot函数

热门文章

  1. uni-app应用内跳转至app-store
  2. apple耳机与nokia耳机内部电路
  3. Python——极客战记codecombat关卡代码
  4. Node.js 与 WebAssembly
  5. 【Ceres】(三)Covariance Estimation
  6. HTM5笔记——地理位置
  7. 【服装行业】服装ERP管理系统的应用优势
  8. C++题目:厄运金币
  9. 对抗生成网络学习(四)——WGAN+爬虫生成皮卡丘图像(tensorflow实现)
  10. 炒菜机器人的弊端_炒菜机器人好用吗,使用有什么优势吗?