Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 772
user71346 Avatar asked Sep 02 '25 04:09

user71346


1 Answers

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

like image 103
Leukonoe Avatar answered Sep 04 '25 17:09

Leukonoe