Secure random bytes and unbiased sampling for MoonBit; never a seeded generator.
test "draw" {
let key = @moonrand.bytes(32, @system.source) // 32 uniform bytes
let n = @moonrand.below(b"\x03\xe8", @system.source) // uniform in [0, 1000)
let d = @moonrand.scalar(b"\x05", @system.source) // uniform in [1, 4]
ignore((key, n, d))
}| Package | What it holds | Depends on |
|---|---|---|
| moonrand | Source, Unavailable, bytes, below, scalar, Replay | nothing |
| moonrand/system | source, the platform's entropy | core env |
| Source | Where the bytes come from |
|---|---|
| @system.source | crypto.getRandomValues on JavaScript, WASI random_get on wasm and wasm-gc, the runtime on native |
| Replay::new(bytes) | the bytes given, in order; exhausted after |
| your own | anything that implements Source |
struct Device {}
impl @moonrand.Source for Device with fn fill(_self, _buf) {
raise Failed("the device is unplugged")
}pub struct Replay {
// private fields
}Install
Download zipSecure random bytes and unbiased sampling for MoonBit; never a seeded generator.