baml.random.Rng

A source of random bytes.

Reference version

Signature

interface baml.random.Rng

A source of random bytes.

Three implementations are provided by the standard library:

  • SystemRandom is a secure singleton provider that calls out to the system's entropy source
  • Xoshiro256PlusPlus is a fast seedable PRNG
  • ChaCha20 is a secure seedable PRNG

Source:<builtin>/baml/ns_random/random.bamlbytes 2901648

Required instance methods

function

random

(self, bytes: int) -> uint8array throws never

Produces bytes uniformly random bytes.

Panics

  • If bytes is negative.
  • If bytes bytes cannot be allocated (baml.panics.AllocFailure).
  • If the implementation fails to produce the requested number of bytes (for example, if the underlying system random number generator is unavailable)

Default instance methods

function

random_int

(self) -> int throws never

Returns a uniformly random int over the full [int.min_value(), int.max_value()] range.

BAML int is 63-bit, so this discards a single bit of the 64 drawn (the top bit of the first byte). The remaining 63 bits are uniform, and so is the value they spell.