Generate 100 normally distributed random numbers in Python

吃可爱长大的小学妹 提交于 2020-02-22 08:14:13

问题


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!


回答1:


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




回答2:


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)]



回答3:


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)



回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/36130038/generate-100-normally-distributed-random-numbers-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!