简体   繁体   中英

Generate 100 normally distributed random numbers in Python

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!

import numpy as np

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

here you can find documentation for normal distribution using numpy: http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.random.normal.html

then you can calculate mean by using: my_mean_value = np.mean(L)

you have to remember, that if you want to print something, you need to use print my_mean value

uniformrandom() is not returning anything. You need to add a return statement:

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

That is returning the number of gauss 's, though. You aren't even storing the gauss() calls in a list. You should change your function to this:

def uniformrandom(n):
    gausses = []
    for _ in range(n):
        gausses.append(gauss(1.0, 0.005))
    return gausses

You could even use a list comprehension:

def uniformrandom(n):
    return [gauss(1.0, 0.005) for _ in range(n)]

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.

You just indent using spaces.

I tried the code in Python but L does not print anything.

Well, your function did not return any value. Edit now your code returns an integer i , which will be the same as n . It still doesn't return anything to do with the gauss function you call.

For example, to generate a single suitable number:

def uniformrandom_1():
    return gauss(1.0, 0.005)

now, if you want a list of n numbers, you can just use

[uniformrandom_1() for i in range(n)]

or write that as a function:

def uniformrandom(n):
    return [uniformrandom_1() for i in range(n)]

how can I store those numbers and use the formula to get the mean


OK, now we can translate your mean formula into

def mean(sample):
    return sum(sample)/len(sample)

L = uniformrandom(100)
LMean = mean(L)

Try the following code,

def uniformrandom(n):  
   nums=[]
   total=0
   i=0  
   for i in range(1, n):
     num=gauss(1.0, 0.005)
     nums.append( num  ) 
     total +=num
  return (nums,total/n)

returns the generated numbers and the mean

This will create a list with n random numbers using gauss. import random as rd

def uniformrandom(n):  
    i=0
    random_list=[]*n
    for i in range(n):  
        random_list+=[rd.gauss(1, 0.005)]
    return random_list

to get the mean simply use mean=sum(random_list)/n

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 usenumpy.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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM