Python 中的 random.uniform( ) 函数教程与实例解析

random.uniform( ) 函数教程与实例解析

1. uniform( ) 函数说明

random.uniform(x, y) 方法将随机生成一个实数,它在 [x,y] 范围内。

2. uniform( ) 的语法与参数

2.1 语法

# _*_ coding: utf-8 _*_
import random
random.uniform(x, y)

# _*_ coding: utf-8 _*_
from random import uniform
uniform(x, y)

提示:uniform 包含在random库中,需要使用时需导入random库。

2.2 参数

  • x -- 随机数的最小值,包含该值。
  • y -- 随机数的最大值,不包含该值。
  • 返回一个浮点数

3. 实例

例程:

# _*_ coding: utf-8 _*_
import random
print("uniform(1 ,  6) 的随机返回值为 : ",  random.uniform(1 ,  6))

print("uniform(10, 16) 的随机返回值为 : ",  random.uniform(10, 16))

运行结果:

#uniform(1 ,  6) 的随机返回值为 :  3.001161523486847
#uniform(10, 16) 的随机返回值为 :  13.70906147017741

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值