Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 53 / 68

Generating random numbers

This lesson covers generating random numbers with Julia's rand, with the aim of building things with a random element such as dice or rock-paper-scissors.

rand() picks values at random from a range or a collection. Use it for dice, lotteries, and the like — the result changes on every run.

The sample code picks a dice roll from 1 to 6 with rand(1:6) and one of Rock, Scissors, or Paper from an array with rand(choices). Note the consistency of the same rand() serving both a range and a collection.

A common early stumble is how many things rand() accepts. Its behaviour changes with what you pass — a range, an array, a type — so it is worth confirming you are using it as intended.

In professional work, this is indispensable wherever you want an unpredictable element — shuffling in a game, sampling in a simulation.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)