6.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].

6.9.1.Β Random Generators

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

Interface for random number generators.

Instance Constructor

RandomGen.mk.{u} {g : Type u} (range : g β†’ Nat Γ— Nat) (next : g β†’ Nat Γ— g) (split : g β†’ g Γ— g) : RandomGen g

Methods

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.

range:g β†’ Nat Γ— Nat

range returns the range of values returned by the 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 (s1 s2 : Nat) : StdGen

Fields

s2:Nat
s1: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.

6.9.2.Β System Randomness

πŸ”—
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.