Random

Luma provides general-purpose random number generation through the random namespace.
Generate random integers, floats, and booleans for games, sampling, testing, and simulations.
For cryptographically secure random bytes, use crypto.rand() instead.

Random integer

n: int = random.int(min, max)

Returns a random integer in [min, max] — both ends inclusive.

// Dice roll (1 to 6)
roll: int = random.int(1, 6)
print("Rolled: ${roll}")

// Random index into a list
names: [str] = ["Alice", "Bob", "Carol"]
i: int = random.int(0, names.len() - 1)
print("Winner: ${names[i]}")

Both-inclusive range matches how people think about dice rolls and card draws — random.int(1, 6) can return 1, 2, 3, 4, 5, or 6. No off-by-one surprises.

Panics if min > max.

Random float

f: float = random.float(min, max)

Returns a random float in [min, max) — min inclusive, max exclusive.

// Probability (0.0 to 1.0)
chance: float = random.float(0.0, 1.0)
print("Chance: ${chance}")

// Temperature between 36.0 and 38.0
temp: float = random.float(36.0, 38.0)
print("Temp: ${temp}")

Half-open range is the standard convention across all languages — max can never actually be returned for floats.

Panics if min > max.

Random boolean

b: bool = random.bool()

Returns true or false with equal probability.

// Coin flip
heads: bool = random.bool()
if heads {
    print("Heads!")
} else {
    print("Tails!")
}

Examples

Dice roller

(1..=5).walk(i) -> {
    print("Roll ${i}: ${random.int(1, 6)}")
}

Random password generator

chars: str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
password: str = ""
(1..=16).walk(i) -> {
    idx: int = random.int(0, chars.len() - 1)
    password = password + chars[idx]
}
print("Password: ${password}")

Weighted coin flip

// 70% chance of heads
fn weighted_flip(probability: float) -> bool {
    return random.float(0.0, 1.0) < probability
}

if weighted_flip(0.7) {
    print("Heads (70% chance)")
}

Shuffle simulation

fn pick_random(items: [str]) -> str {
    i: int = random.int(0, items.len() - 1)
    return items[i]
}

colors: [str] = ["red", "green", "blue", "yellow"]
print("Picked: ${pick_random(colors)}")

Playground

Try random functions live in the Luma Playground :

  • Random: Integer (Dice Roll) — generate random integers in a range
  • Random: Float (Probability) — generate random floats for probability and sampling
  • Random: Bool (Coin Flip) — random true/false decisions

Method summary

Method Arguments Returns Description
random.int() min: int, max: int int Random integer in [min, max]
random.float() min: float, max: float float Random float in [min, max)
random.bool() none bool Random true or false

Error handling

Operation Behavior
random.int(min, max) Panics if min > max
random.float(min, max) Panics if min > max
random.bool() Always succeeds

Design notes

  • random is a namespace, not a type — functions are stateless, no object to create
  • Uses math/rand/v2 internally — auto-seeded, no rand.Seed() or setup needed
  • Not cryptographically secure — for security-sensitive randomness, use crypto.rand() which provides OS-level secure random bytes
  • Both-inclusive for intrandom.int(1, 6) returns 1 through 6, matching natural expectations for dice rolls and card draws
  • Half-open for floatrandom.float(0.0, 1.0) returns [0.0, 1.0), the universal convention for floating-point random generation
  • No seeding API — deterministic seeding can be added later if needed for reproducible testing
Last updated on