Rand Monad and Random Class #
This module provides tools for formulating computations guided by randomness and for defining objects that can be created randomly.
Main definitions #
RandT
andRandGT
monad transformers for computations guided by randomness;Rand
andRandG
monads as special cases of the aboveRandom
class for objects that can be generated randomly;random
to generate one object;
BoundedRandom
class for objects that can be generated randomly inside a range;randomR
to generate one object inside a range;
runRand
to run a randomized computation inside any monad that has access tostdGenRef
.
References #
- Similar library in Haskell: https://hackage.haskell.org/package/MonadRandom
A monad to generate random objects using the generator type g
.
Equations
Instances For
A monad to generate random objects using the generator type StdGen
.
Equations
Instances For
Equations
- Plausible.instMonadLiftTRandGTOfMonadLift = { monadLift := fun {α : Type ?u.38} (x : Plausible.RandGT g m α) (s : ULift g) => liftM (x s) }
Random m α
gives us machinery to generate values of type α
in the monad m
.
Note that m
is a parameter as some types may only be sampleable with access to a certain monad.
- random : {g : Type} → [inst : RandomGen g] → Plausible.RandGT g m α
Generate a value of type
α
randomly using generatorg
.
Instances
BoundedRandom m α
gives us machinery to generate values of type α
between certain bounds in
the monad m
.
- randomR : {g : Type} → (lo hi : α) → lo ≤ hi → [inst : RandomGen g] → Plausible.RandGT g m { a : α // lo ≤ a ∧ a ≤ hi }
Generate a value of type
α
betweenlo
andhi
randomly using generatorg
.
Instances
Given a random generator for α
, we can convert it to a random generator for ULift α
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Given a random generator for ULift α
, we can convert it to a random generator for α
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Generate one more Nat
Equations
- Plausible.Rand.next = do let __do_lift ← get let rng : g := __do_lift.down match RandomGen.next rng with | (res, new) => do set { down := new } pure res
Instances For
Create a new random number generator distinct from the one stored in the state
Equations
- Plausible.Rand.split = do let __do_lift ← get let rng : g := __do_lift.down match RandomGen.split rng with | (r1, r2) => do set { down := r1 } pure r2
Instances For
Get the range of Nat that can be generated by the generator g
Equations
- Plausible.Rand.range = do let __do_lift ← get let rng : g := __do_lift.down pure (RandomGen.range rng)
Instances For
Given a random generator for α
, we can convert it to a random generator for ULift α
.
Equations
- Plausible.Rand.up x = Plausible.RandT.up (fun {α : Type ?u.32} (x : Id α) => pure { down := x.run }) x
Instances For
Given a random generator for ULift α
, we can convert it to a random generator for α
.
Equations
- Plausible.Rand.down x = Plausible.RandT.down (fun {α : Type ?u.32} (x : Id (ULift α)) => pure x.run.down) x
Instances For
Generate a random value of type α
.
Equations
- Plausible.Random.rand α = Plausible.Random.random
Instances For
Generate a random value of type α
between x
and y
inclusive.
Equations
- Plausible.Random.randBound α lo hi h = Plausible.BoundedRandom.randomR lo hi h
Instances For
Generate a random Fin
.
Equations
- Plausible.Random.randFin { down := g_1 } = pure (Prod.map (Fin.ofNat' n.succ) ULift.up (randNat g_1 0 n))
Instances For
Generate a random Bool
.
Equations
- Plausible.Random.randBool = do let __do_lift ← Plausible.Random.rand (Fin 2) pure (__do_lift == 1)
Instances For
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Computes a RandT m α
using the global stdGenRef
as RNG.
Note that:
stdGenRef
is not necessarily properly seeded on program startup as of now and will therefore be deterministic.stdGenRef
is not thread local, hence two threads accessing it at the same time will get the exact same generator.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Run the random computaton cmd
with seed
for the RNG.
Equations
- Plausible.runRandWith seed cmd = do let __do_lift ← StateT.run cmd { down := mkStdGen seed } pure __do_lift.fst