pythonrandom中的随机函数
pythonrandom中的随机函数
Python标准库的random函数可以生成随机浮点数、整数、字符串,也可以随机选择列表序列的要素,打乱数据组等。
1、seed与random函数
seed函数初始化一个随机种子,默认是当前系统时间。
random函数生成一个[0.0,1.0)之间的随机小数。
importrandom
random.seed(10)
x=random.random()
print(x)
2、randint
参数1、参数2必须是整数,函数返回参数1和参数2之间的任意整数。
importrandom
defrand_int(m,n):
delta=n-m
returnround(random.random()*delta+m)
print(rand_int(1,10))
以上就是pythonrandom中随机函数的介绍,希望对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python包的导入方式有几种
python包的导入方式有几种本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、from...import导入frompackageimportmodule1,mo详情>>
2023-11-08 23:58:44
python参数是什么
python参数是什么1、概念Python中函数的参数是不变的对象。Python函数具有灵活的参数形式,但默认参数必须指向不变的对象。如果默认参数是可变...详情>>
2023-11-08 19:48:30
python使用items()遍历键值对
python使用items()遍历键值对字典可以用来存储各种方式的信息,所以有很多方式可以通过字典的所有键值对、键或值。说明1、即使通过字典,键值对...详情>>
2023-11-08 19:23:48
python按行读取文件的方法比较
python按行读取文件的方法比较1、read方法默认会把文件的所有内容一次性读取到内存。如果文件太大,对内存的占用会非常严重2、readline方法,re...详情>>
2023-11-08 18:22:27