AndyC
AndyC

Reputation: 59

Log Normal Distribution within a Range

I'm trying to figure out how to create a log-normal distribution in python within in range of days for a modelling project that I'm working on. I have researched this however; not really found anything that I can use. I do not have Mean and STD Dev just a range between 50 and 250.

I have some code that I have been playing with:

np.random.seed(42)
samples = np.random.lognormal(mean=0, sigma=1, size=500)
samples = samples * 26.4
samples

Using this I have been able to experiment with the mean, sigma and samples multiplier to get a distribution that roughly looks like what I'm after however; I'm wondering if there's a cleaner way using a range min - max.

Has anyone had a similar problem/need? Can you help with a code snippet please?

Thanks in advance!

Upvotes: 0

Views: 174

Answers (1)

Damien Pageot
Damien Pageot

Reputation: 61

I found this piece of code in an old file of mine. I don't know if it can help you...

from math import log
from random import random

def mylognorm(vmin, vmax):
    vrand = random()
    logratio = log(vmax) / log(vmin)
    power = ((logratio-1.) * vrand)+1.
    return vmin**power

vmin = 4.
vmax = 250.

print(mylognorm(vmin, vmax))

Upvotes: 2

Related Questions