Skip to the content.

Home › Language Reference › Random

Random

Aether provides two built-in random number functions: random() for a float and rand_int(n) for an integer.

random()

Returns a random float in the range [0, 1).

let r = random()
println(r)  // 0.7423619...

rand_int(n)

Returns a random integer in the range [0, n). n must be a positive int.

rand_int(6)    // int in [0, 6) — e.g. dice roll
rand_int(2)    // 0 or 1 — coin flip

Examples

Dice roll

fn main() {
    let roll = rand_int(6) + 1
    println("You rolled a ${roll}")
}

Random element from an array

fn choice(arr) {
    return arr[rand_int(len(arr))]
}

fn main() {
    let colors = ["red", "green", "blue"]
    println(choice(colors))
}

Limitations


← Time    Async →