mercredi 16 septembre 2020

How to get a list of random numbers between two numbers with a specified mean and standard deviation (in Python)?

I have two outer limits, a mean and a standard deviation value. I need to generate a specified number of random decimal numbers within the two limits included having the specified mean and standard deviation value. I have written two code blocks which I have found on the Internet. But crosschecking by finding the mean of the list, I found that the mean obtained was different. How to get the correct mean and standard deviation value?

One piece of code is:

import scipy.stats as stats

a, b = 27.6, 59.4
mu, sigma = 32, 54
dist = stats.truncnorm((a - mu) / sigma, (b - mu) / sigma, loc=mu, scale=sigma)

agesp = dist.rvs(419)

The other piece of code is:

values = []
while len(values) < 419:
    sample = random.gauss(32, 54)
    if sample >= 27.6 and sample < 59.4:
        values.append(sample)



Aucun commentaire:

Enregistrer un commentaire