Home > front end >  How to generate a normally distributed variable in Python?
How to generate a normally distributed variable in Python?

Time:11-19

I have a list of 10 values:

variable=[2.1, 5.3, 4.1, 6.7, 2, 6.6, 1.9, 4.51, 4, 7.15]

Its length:

>>> len(variable)
10

Its average:

>>> mean(variable)
4.436

Its standard deviation:

>>> np.std(variable)
1.8987269419271429

From it I want to generate a new_variable having len(new_variable)==100 and normally distributed where mean==4.436 and np.std==1.898.

CodePudding user response:

You can use the random.gauss function:

1 sample:

import random
x = random.gauss(4.436, 1.898)

or 100 samples:

import random
x = [random.gauss(4.436, 1.898) for _ in range(100)]

This is standard library, you don't need to install anything. You may also be interested in the statistics library.

CodePudding user response:

This function achieves exactly what you asked for by transforming a randomly generated distribution.

from statistics import NormalDist, mean, stdev

def get_target_dist(target_mean, target_std, size):
    dist = NormalDist(target_mean, target_std).samples(size)
    dist_mean, dist_std = mean(dist), stdev(dist)
    dist_standard = [(val - dist_mean) / dist_std for val in dist]
    dist_scaled = [val * target_std   target_mean for val in dist_standard]
    return dist_scaled

dist = get_target_dist(4.436, 1.898, 100)

print("len:", len(dist))
print("mean:", mean(dist))
print("std:", stdev(dist))

# len: 100
# mean: 4.436
# std: 1.898
  • Related