python random.uniform()的作用,获取指定区间随机数

random.uniform()方法

python模块random的uniform()方法,可以用于随机获取参数,比如a和b指定的区间内的浮点数,并且a和b的值都有可能被获取到。其python源码中的介绍如下:

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(a,b)

提示:如果a大于b,则获取到的值是介于b到a之间的数,如果a小于b,则是a到b的数。


返回值

python的float类型值,介于参数指定的区间之内。


uniform()实例代码

>>> import random
>>> random.uniform(1,2) #参数a小于b
1.813354387402276
>>> random.uniform(6,5) #参数a大于b
5.842881711313225

全栈后端 / Python库 :









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