Skip to content

Random

Generate random values — integers, floats, strings, UUIDs.

Setup

Call Random.seed_auto() once at the start of your program for good randomness (seeds from /dev/urandom). Without it, you get srand(0) which produces the same sequence every run.

wyn
fn main() {
    Random.seed_auto()
    println(Random.int(1, 100))
}

API

MethodDescription
Random.int(min, max) -> intRandom integer in range [min, max] inclusive
Random.float() -> floatRandom float in [0.0, 1.0)
Random.bool() -> boolRandom true/false
Random.string(len) -> stringRandom alphanumeric string of given length
Random.hex(len) -> stringRandom hex string of given length
Random.uuid() -> stringRandom UUID v4
Random.range(min, max) -> intAlias for Random.int
Random.seed_auto()Seed from /dev/urandom (call once at startup)

Examples

wyn
Random.seed_auto()

// Random ID for a session
var session_id = Random.hex(32)

// Random password
var password = Random.string(24)

// Coin flip
if Random.bool() {
    println("heads")
} else {
    println("tails")
}

// Dice roll
var die = Random.int(1, 6)

// Random UUID
var id = Random.uuid()
println("User ID: ${id}")

MIT License