8.9.Β Random Numbers

πŸ”—def
IO.setRandSeed (n : Nat) : BaseIO Unit
πŸ”—def
IO.rand (lo hi : Nat) : BaseIO Nat
πŸ”—def
randBool.{u} {gen : Type u} [RandomGen gen]
    (g : gen) : Bool Γ— gen

Generate a random Boolean.

πŸ”—def
randNat.{u} {gen : Type u} [RandomGen gen]
    (g : gen) (lo hi : Nat) : Nat Γ— gen

Generate a random natural number in the interval [lo, hi].

8.9.1.Β Random Generators

πŸ”—type class
RandomGen.{u} (g : Type u) : Type u

Interface for random number generators.

Instance Constructor

RandomGen.mk.{u}

Methods

range : g β†’ Nat Γ— Nat

range returns the range of values returned by the generator.

next : g β†’ Nat Γ— g

next operation returns a natural number that is uniformly distributed the range returned by range (including both end points), and a new generator.

split : g β†’ g Γ— g

The 'split' operation allows one to obtain two distinct random number generators. This is very useful in functional programs (for example, when passing a random number generator down to recursive calls).

πŸ”—structure
StdGen : Type

"Standard" random number generator.

Constructor

StdGen.mk

Fields

s1 : Nat
s2 : Nat
πŸ”—def
stdRange : Nat Γ— Nat
πŸ”—def
stdNext : StdGen β†’ Nat Γ— StdGen
πŸ”—def
stdSplit : StdGen β†’ StdGen Γ— StdGen
πŸ”—def
mkStdGen (s : Nat := 0) : StdGen

Return a standard number generator.

8.9.2.Β System Randomness

πŸ”—opaque
IO.getRandomBytes (nBytes : USize)
    : IO ByteArray

Read bytes from a system entropy source. Not guaranteed to be cryptographically secure. If nBytes = 0, return immediately with an empty buffer.