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()
    print(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
session_id = Random.hex(32)

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

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

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

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

See Also

  • Math - math functions
  • Arrays - shuffle arrays
  • Crypto - cryptographic hashing
  • Bcrypt - password hashing with random salts

MIT License - v1.21.0