python顶用random模块来生成随机数。
random模块的几种用法以下
1.随机浮点数
random() --- 发生大于即是0且小于1的浮点数
ret = random.random() print(ret)
uniform(a,b) --- 发生指定局限的随机浮点数
ret = random.uniform(1, 4) print(ret)
2.随机整数
randint(a,b) --- 发生a,b局限内的整数,包括开首和末端
randrange(start,stop,[step]) --- 发生start,stop局限内的整数,包括开首不包括末端,step指定发生随机数的步长。
ret = random.randrange(1, 6, 2) print(ret)
3.随机挑选一个数据
random.choice(lst) --- 随机返回序列中的一个数据
lst = ['a', 'b', 'c'] ret = random.choice(lst) print(ret)
4.打乱
shuffle() --- 打乱列表的递次
lst = ['a', 'b', 'c'] print(lst) # ['a', 'b', 'c'] random.shuffle(lst) print(lst) # ['b', 'a', 'c']
以上就是python怎样生成整数局限内的随机数的细致内容,更多请关注ki4网别的相干文章!