Wednesday, April 11, 2012

How to generate an array of random numbers following a normal distribution with arbitrary mean and standard deviation

Here is a recipe to generate an array of random numbers following a normal distribution with the supplied mean and standard deviation in Python:

 def random_normal(mean,std,n):  
      """  
 Returns an array of n elements of random variables, following a normal   
 distribution with the supplied mean and standard deviation.  
      """  
      import scipy  
      return std*scipy.random.standard_normal(n)+mean  

Update Oct. 23rd 2014: code snippet available on github

2 comments:

  1. where did you get the nemmen package from though???

    ReplyDelete
  2. There is no reference to this package above, but the nemmen package was renamed to nmmn and is available here: https://github.com/rsnemmen/nmmn

    ReplyDelete