# 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.9967897533005654

rand()               # Different output each time

0.7381472548909045

rand(3)              # Array of 3 random numbers

3-element Vector{Float64}:
0.7534762291820414
0.9536035678091033
0.5544324956840205


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.5908446386657102, 0.7667970365022592, 0.5662374165061859]
[0.4600853424625171, 0.7940257103317943, 0.8541465903790502, 0.20058603493384108, 0.2986142783434118]
[0.5908446386657102, 0.7667970365022592]
[0.5662374165061859, 0.4600853424625171, 0.7940257103317943, 0.8541465903790502, 0.20058603493384108, 0.2986142783434118]


## 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.506325626779982
0.15934446673808322
0.29776390041869094
-0.9781882207288093
-0.8671539260893253

z = 7rand(3) .+ 4    # 3 uniform random numbers on [4,11]

3-element Vector{Float64}:
10.697273545220465
8.526836870721521
4.7874010983099815


## 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}:
7
8
9


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
-1.0
3.141592653589793
-1.0
2.718281828459045


## 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()

-0.2511757400198831


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.8485701751587267