python random.randint(),返回随机整数

random.randint()方法

python random模块中的randint()方法,可以用于参数指定区间的随机整数,而且两个参数的值也有可能被获取到。


语法

random.randint( a, b )

python源码对randint()方法的介绍:

 method of random.Random instance
    Return random integer in range [a, b], including both end points.

参数

参数描述
a必须参数,指的是区间的最小值,而且该值也可以被获取到
b必须参数,指的是区间的最大值,而且该值也可以被获取到

返回值

python int整型数值,介于参数指定的[a, b]区间。


randint()实例代码

>>> import random
>>> random.randint(5,9)
5
>>> random.randint() #该方法必须接收两个参数,否则python抛出TypeError
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: randint() missing 2 required positional arguments: 'a' and 'b'

全栈后端 / Python库 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.