Generate 100 normally distributed random numbers in Python

Viewed 21182

I am an extreme beginner in Python and I am having a difficulty writing a very simple code.

I am trying to write a simple code to generate 100 normally distributed number by using the function gauss with expectation 1.0 and standard deviation 0.005, and later store in an array that can be used to calculate the mean and standard deviation from those 100 sample.

Here is my code:

def uniformrandom(n):   
    i=0  
    while i< n:  
        gauss(1.0, 0.005)  
        i = i + 1
    return i

Then I tried

L = uniformrandom(100)

The code is supposed to be indented in Python but it is just when I typed in StackOverflow I didn't really know how to indent it.

Let say I use the formula (x1+x2+...+xn)/100 to get the mean, how can I store those numbers and use the formula to get the mean.

I tried the code in Python but L only prints the value n. I have little what is wrong with my code and how should I fix it.

If anyone could lend some help, it would be really appreciated. Thanks so much!

6 Answers

In order to generate 100 normally distributed random numbers in Python by using the function gauss with expectation 1.0 and standard deviation 0.005, one can use numpy.random.normal as follows

import numpy as np

random_numbers = np.random.normal(1.0, 0.005, 100)

In order to store the random_numbers in an array, one can do that with numpy.array as follows

random_numbers_array = np.array(random_numbers)

Then to calculate the mean use numpy.mean

mean = np.mean(random_numbers_array)

And to calculate the standard deviation use numpy.std as follows

std = np.std(random_numbers_array)

A function, that takes as input the mean and std of the random number generator, and does what OP wants, can be something like

def uniformrandom(mean, std):
    random_numbers = np.random.normal(mean, std, 100)
    random_numbers_array = np.array(random_numbers)
    mean = np.mean(random_numbers_array)
    std = np.std(random_numbers_array)
    return random_numbers_array, mean, std

Let's see what it retrieves

print(uniformrandom(1.0, 0.005))

[Out]:
(array([1.00716042, 0.99938042, 0.99178698, 1.00791888, 1.00623344,
       1.00555578, 0.99890757, 1.00695046, 0.98482516, 0.9928371 ,
       1.00016377, 0.99510195, 1.00280951, 0.99472607, 0.99453582,
       1.00791222, 1.00302319, 1.00004503, 0.99884054, 1.00429994,
       0.99591756, 1.010769  , 1.00827643, 0.996754  , 0.99236853,
       1.00096622, 1.00092158, 1.00192217, 1.00148108, 0.9975529 ,
       1.00953799, 1.0073464 , 0.99942883, 1.0065139 , 1.00265884,
       1.00885268, 0.99613224, 1.00299541, 0.99977556, 1.01090735,
       1.00132776, 0.99711267, 1.00129545, 1.00500702, 0.99937595,
       1.00603761, 0.98960716, 0.99932355, 0.99687272, 1.00332839,
       0.991147  , 0.99643908, 0.99279811, 1.00112179, 1.00012058,
       0.9989405 , 1.00150169, 1.00683601, 0.99885708, 0.99632519,
       1.00112315, 0.99280336, 1.00759542, 1.00140661, 1.00183764,
       0.99540866, 1.0002343 , 0.99421579, 1.01169739, 1.00330142,
       0.99977923, 1.00365608, 0.9984007 , 1.00106568, 1.00349778,
       0.99527499, 0.99189253, 0.99477082, 0.99486919, 0.99784054,
       0.99240925, 1.00417557, 0.99566904, 1.00355492, 0.99717846,
       0.99910477, 0.99718301, 1.00711659, 0.99623698, 1.00143697,
       1.00876763, 1.0049953 , 0.99885742, 0.99498201, 1.00324752,
       0.99907905, 0.99762597, 0.99502917, 0.99511507, 1.00991401]), 1.0002981820807302, 0.005332038881947385)
Related