python lognorm_Python random.lognormvariate()用法及代码示例

random模块用于在Python中生成随机数。实际上不是随机的,而是用于生成伪随机数的。这意味着可以确定这些随机生成的数字。

lognormvariate()

lognormvariate()是内置的方法random模块。它用于返回具有log-normal分布的随机浮点数。

用法: random.lognormvariate(mu, sigma)

参数:

mu:平均

sigma:标准偏差,大于0

返回:随机的log-normal分配浮点数

范例1:

# import the random module

import random

# determining the values of the parameters

mu = 0

sigma = 0.25

# using the lognormvariate() method

print(random.lognormvariate(mu, sigma))

输出:

0.8585439051088984

范例2:我们可以多次生成该数字并绘制图形以观察log-normal的分布。

# import the required libraries

import random

import matplotlib.pyplot as plt

# store the random numbers in a

# list

nums = []

mu = 0

sigma = 0.25

for i in range(100):

temp = random.lognormvariate(mu, sigma)

nums.append(temp)

# plotting a graph

plt.plot(nums)

plt.show()

输出:

60efe9f526e3d94385b53b4f2c831836.png

范例3:我们可以创建一个直方图来观察log-normal分布的密度。

# import the required libraries

import random

import matplotlib.pyplot as plt

# store the random numbers in a list

nums = []

mu = 0

sigma = 0.25

for i in range(10000):

temp = random.lognormvariate(mu, sigma)

nums.append(temp)

# plotting a graph

plt.hist(nums, bins = 200)

plt.show()

输出:

ec00d136eb2806bfb461b36c0e784506.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值