python random.randrange()方法,获取范围内随机整数
random.randrange()方法
python random模块中的randrange()方法,可以用来获取参数指定范围之内的一个随机整数。
语法
random.randrange(start, stop=None, step=1, _int=<class 'int'>)
python源码中对randrange()方法的介绍:
method of random.Random instance
Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
参数
参数 | 描述 |
---|---|
start | 指定范围的起始位置,可以获取到 |
stop | 指定范围的结束位置 |
step | 指定range的步伐 |
randrange()实例代码
>>> import random
>>> random.randrange(1,2)
1
>>> random.randrange(2,9)
3
>>> random.randrange(6)
2