Home » Sampling from a specific distribution in Julia

Sampling from a specific distribution in Julia

  • by
  • 3 min read

Here’s my second blog post on Julia. For a simple simulation I needed some randomly generated numbers from a normal distribution. In Julia, there are multiple ways to generate them. But to really customize the distributions, you need a random number generator (RNG). Because I found the documentation to be somewhat confusing and lacking examples, I give an introductory overview that should get you started.

Using base functions

A quick way to generate random numbers can be done using nothing but Base functions. The rand function generates a uniform distribution and takes (1) a set of values or a generator (see below) and (2) the dimensions that define the output.

rand() # generates a Float64 between 0 and 1
rand(1:10) # generates an Int64 between 1 and 10
rand(100) # generates a vector of 100 Float64s between 0 and 1
rand(1:10,5) # generates a vector of 5 Int64s between 1 and 10
rand(["a","b","c"],25) # generates a vector of 25 strings
rand(Float64,(2,5)) # generates a 2x5 array of 10 Float64s between 0 and 1
rand(2,5) # idem

The generation of numbers from a normal distribution is completely analogous. The randn function generates a normal distribution with mean 0 and standard deviation 1 (= standard normal). It takes a type and the dimensions that define the output.

randn() # generates a Float64, drawn from a standard normal distribution
randn(20) # generates a vector of 20 Float64s, drawn from a standard normal distribution
randn(2,5) # generates a 2x5 array of 10 Float64s, drawn from a standard normal distribution
randn(Float64,10) # generates a vector of 10 Float64, drawn from a standard normal distribution

There’s a base function for the generation of numbers from an exponential distribution too.

Using the Distributions package

If we want to customize our distributions, we can do that easily using the Distrubutions package. It can be installed as follows:

Pkg.add("Distributions")

Generating random numbers from a particular distribution using the Distributions package is a two-step process. First we create the distribution object and then we sample from it. The example below creates a normal distribution with a mean of 5 and a standard deviation of 3. Next, we draw 10 samples from it.

d = Normal(5,3)
rand(d,10)

The same goes for a whole range of other distributions. In the next example, I draw 10 samples from a poisson distribution with lambda 7.

rand(Poisson(7),10)

There’s a load of distributions to be found in Julia’s Distributions package. And if you get lost in its parameters, you can use the fieldnames function. If we use it on the beta distribution, it will tell us to provide an alpha and a beta.

fieldnames(Beta)

There are a lot more distributions, and the Distribution package has a lot more functionalities. But I hope you can now generate random numbers in Julia with a specific distribution.

Great success!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

Leave a Reply

Your email address will not be published. Required fields are marked *