Julia Random Tutorial
last modified November 18, 2026
The Random standard library generates pseudo-random numbers and
random arrangements of data. This tutorial covers floating-point and integer
samples, normally distributed values, permutations, shuffling, and independent
generators.
Randomness is nondeterministic unless a seed is fixed: a seed is the
starting state of the number generator. Every example below uses a fixed seed,
installed either with Random.seed! or by constructing a
MersenneTwister, so the printed output is reproducible. Each listing
is a standalone main.jl file, verified with Julia 1.13.4.
Seeding the Generator
Calling rand() with no arguments produces one floating-point value
uniformly distributed in the interval from 0 up to, but not including, 1.
using Random Random.seed!(42) println(rand()) println(rand()) println(rand())
Random.seed!(42) resets the global generator to a known state, so
the three calls always deliver the same numbers in the same order. Without the
seed, a fresh run would print different values. Change the seed and the sequence
changes completely.
$ julia main.jl 0.6293451231426089 0.4503389405961936 0.47740714343281776
Random Integers and Arrays
Passing a range to rand draws from the values in that range; adding
a dimension produces an array of draws at once.
using Random Random.seed!(42) println(rand(1:6)) println(rand(1:10, 3))
The first line is a single roll of a six-sided die, which happens to give 4. The second line fills a vector with three numbers between 1 and 10; repetitions are allowed, so the value 5 appears twice. Arrays and ranges are introduced in the arrays tutorial.
$ julia main.jl 4 [5, 5, 8]
Normally Distributed Values
The randn function draws from the standard normal distribution: a
bell-shaped distribution centred at zero with a standard deviation of one.
using Random Random.seed!(42) println(round(randn(); digits=4)) println(round.(randn(3); digits=4))
A single draw is about 0.7884. The call randn(3) returns three
draws, and the dot before round broadcasts that function over the
vector so the output stays easy to read. The rounding does not change which
random values were drawn.
$ julia main.jl 0.7884 [-0.8799, -0.8738, -0.7333]
A Private Generator
Instead of seeding the global generator, we can create an independent generator object and pass it explicitly to the random functions.
using Random rng = MersenneTwister(1234) println(round(rand(rng); digits=4)) println(rand(rng, 1:100))
MersenneTwister is a widely used pseudo-random number generator.
Its argument is the seed, so two generators created with the same seed produce
identical streams. Because this generator is a separate object, drawing from it
does not disturb the global generator used by the other examples.
$ julia main.jl 0.5383 62
Reproducing a Sequence
Seeding again at the same point replays exactly the same sequence. This is what makes simulations and tests repeatable.
using Random Random.seed!(7) a = [rand() for _ in 1:3] Random.seed!(7) b = [rand() for _ in 1:3] println(a) println(b) println(a == b)
Both comprehensions draw three values after the same seed was installed. The two vectors are identical and the equality check is true. Re-seeding is the simple way to reproduce a run, while keeping generator objects separate is the way to avoid interference between unrelated parts of a program.
$ julia main.jl [0.24379531091456585, 0.7191824542790012, 0.8108139133625801] [0.24379531091456585, 0.7191824542790012, 0.8108139133625801] true
Random Permutations
A permutation is a rearrangement of all the elements of a collection.
The randperm function returns a random ordering of the numbers from
one up to its argument.
using Random Random.seed!(1) println(randperm(5)) println(randperm(5))
Each result contains every number from 1 to 5 exactly once, in a different order. Consecutive calls consume successive values from the generator, so the second permutation differs from the first even though the stream is fixed by the seed.
$ julia main.jl [2, 3, 4, 5, 1] [3, 2, 1, 5, 4]
Shuffling a Collection
shuffle returns a randomly reordered copy of a collection;
shuffle! reorders the collection in place.
using Random Random.seed!(3) a = collect(1:6) println(shuffle(a)) println(a) println(shuffle!(copy(a)))
The original vector a is unchanged after shuffle(a),
which confirms that the function makes a copy. The in-place variant returns the
modified vector itself; here it is applied to a copy so that the earlier result
stays intact.
$ julia main.jl [5, 1, 3, 4, 6, 2] [1, 2, 3, 4, 5, 6] [2, 1, 3, 6, 4, 5]
Random Subsequences
randsubseq keeps each element of a collection independently with a
given probability. Unlike randperm, the result can have any length.
using Random Random.seed!(10) a = collect(1:20) s = randsubseq(a, 0.25) println(s) println(length(s))
With probability 0.25, about a quarter of the twenty elements are selected; here seven survive. The chosen elements keep their original relative order. Because the seed is fixed, both the selection and its length are reproducible.
$ julia main.jl [2, 3, 4, 12, 13, 14, 16] 7
Best Practices for Random Numbers
A few rules keep random code predictable and fair.
- Seed only when you need repeatability: tests and published examples should use a fixed seed; production randomness should not.
- Use separate generators in independent components: a
private
MersenneTwisteravoids hidden coupling through global state. - Choose the right primitive: use
randpermorshufflewhen every element must appear exactly once, andrandsubseqwhen elements may be skipped. - Do not assume a length:
randsubseqcan return an empty collection even with a high probability.
Source
Julia Random standard library documentation
In this article we have generated random values with rand and
randn, built permutations and shuffled vectors, sampled
subsequences, and reproduced sequences with Random.seed! and
MersenneTwister. Summary statistics for sampled data are covered in
Julia statistics, and arrays are described in
Julia arrays.
Author
List all Julia tutorials.