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
| Method | Description |
|---|---|
Random.int(min, max) -> int | Random integer in range [min, max] inclusive |
Random.float() -> float | Random float in [0.0, 1.0) |
Random.bool() -> bool | Random true/false |
Random.string(len) -> string | Random alphanumeric string of given length |
Random.hex(len) -> string | Random hex string of given length |
Random.uuid() -> string | Random UUID v4 |
Random.range(min, max) -> int | Alias 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}")