lily/docs/spec/rand.md
koniifer fd42e53ae5 lots of work again (mostly broken on libc target)
implement foldhash
implement hashmap
squash & log some more bugs
clean up Target & Config interfaces
add rudimentary math functions
extract allocators to new directory
implement vec.get_ref
fix capitalisation
document some of lily's type spec
2025-01-10 19:32:27 +00:00

1.3 KiB

random number generators

  1. rngs must not allocate on the heap.
  2. the seed of the rng must not change (unless required by the algorithm)
  3. all spec compliant rngs should implement:

unless otherwise stated, functions can be optionally inline.
names of arguments are up to programmer discretion.
names and signature of functions must be identical to shown below.

Random := struct {
    new := fn(seed: uint): Self
    /// prepare to be deallocated
    deinit := fn(self: ^Self): void
    /// should always use a constant or randomised seed
    /// randomised seeds should always use lily.Target.getrandom()
    /// never use values from the environment
    default := fn(): Self
    /// array|slice: compile error
    /// pointer: treat as uint
    /// bool: randomise the least significant bit
    /// other: randomise
    any := fn(self: ^Self, $T: type): T
    /// clamp between min and max (inclusive)
    range := fn(self: ^Self, min: @Any(), max: @TypeOf(min)): @TypeOf(min)
    /// ^array: fill to @lenof(@ChildOf(@TypeOf(buf))) with random bytes
    /// slice: fill to buf.len with random bytes
    /// other: compile error
    fill := fn(self: ^Self, buf: @Any()): void
    /// same as fill, except fill each index with a random value rather than filling the whole buffer with random bytes
    fill_values := fn(self, ^Self, buf: @Any()): void
}