5.1. Distributions#

5.1.1. Uniformly distributed random numbers#

The function rand(n) generates \(n\) so-called pseudo-random numbers from a uniform distribution on \([0,1]\). This means that the numbers are technically fully deterministic, but for most practical purposes they can be seen as random. These numbers have a wide range of application in computing, for statistical simulations, studying noise, etc.

rand()               # Defaults to n = 1 random number
0.04269884999572138
rand()               # Different output each time
0.15458849955064957
rand(3)              # Array of 3 random numbers
3-element Vector{Float64}:
 0.7803073526830069
 0.7657983465001919
 0.8438909594699471

Note that all the generated numbers are different, which we would expect from a random number generator. However, sometimes it is useful to be able to generate the same sequence of random numbers (e.g. for debugging code). This can be done using the Random.seed! command from the Random package, and an arbitrary so-called seed number:

using Random
Random.seed!(1234)      # Seed number 1234
println(rand(3))
println(rand(5))
Random.seed!(1234)      # Re-seed with same number - will give the same sequence of random numbers
println(rand(2))
println(rand(6))
[0.32597672886359486, 0.5490511363155669, 0.21858665481883066]
[0.8942454282009883, 0.35311164439921205, 0.39425536741585077, 0.9531246272848422, 0.7955469475347194]
[0.32597672886359486, 0.5490511363155669]
[0.21858665481883066, 0.8942454282009883, 0.35311164439921205, 0.39425536741585077, 0.9531246272848422, 0.7955469475347194]

5.1.2. Other intervals#

To generate uniform random numbers from a different interval \([a,b]\), you can simply scale a random number \(x\in [0,1]\) by \(a + x(b-a)\). For example:

y = 2rand(5) .- 1     # 5 uniform random numbers on [-1,1]
5-element Vector{Float64}:
 -0.011500266219158783
  0.4968300437749482
  0.1564638931227953
  0.4558700024532112
 -0.98510398773427
z = 7rand(3) .+ 4    # 3 uniform random numbers on [4,11]
3-element Vector{Float64}:
 5.395636298694089
 7.0747018781728785
 8.777728635836691

5.1.3. Random integers#

The rand function can also be used with a range or a vector, from which it chooses samples at random. This can be used to generate random integers:

rand(1:10, 3)        # 3 random integers between 1 and 10
3-element Vector{Int64}:
 10
  7
 10

but also more generally to select any objects from a vector at random:

rand([π, exp(1), -1.0], 5)
5-element Vector{Float64}:
 -1.0
  3.141592653589793
  2.718281828459045
  2.718281828459045
  3.141592653589793

5.1.4. Normal distributions#

The function randn is similar to rand but draws random numbers from a normal distribution with mean 0 and standard deviation 1.

randn()
-1.973077837133343

Suppose \(\mu\) and \(\sigma > 0\) are constants. If \(Z\) is standard normal, then \(\sigma Z + \mu\) is normal with mean \(\mu\) and standard deviation \(\sigma\). Thus we can generate numbers from any normal distributions using the randn function. As an example, the code below draws from a normal distribution with mean 2 and standard deviation 5.

μ = 2
σ = 5
σ*randn() + μ
3.608700549523313