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 #
RandTandRandGTmonad transformers for computations guided by randomness;RandandRandGmonads as special cases of the aboveRandomclass for objects that can be generated randomly;randomto generate one object;
BoundedRandomclass for objects that can be generated randomly inside a range;randomRto generate one object inside a range;
IO.runRandto run a randomized computation inside any monad that has access tostdGenRef.
References #
- Similar library in Haskell: https://hackage.haskell.org/package/MonadRandom
instance
instMonadLiftTRandGTOfMonadLift
{m : Type u_1 → Type u_2}
{n : Type u_1 → Type u_3}
{g : Type}
[MonadLift m n]
:
MonadLiftT (RandGT g m) (RandGT g n)
Equations
- instMonadLiftTRandGTOfMonadLift = { monadLift := fun {α : Type ?u.27} (x : RandGT g m α) (s : ULift.{?u.27, 0} 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.
Sample an element of this type from the provided generator.
Instances
Create a new random number generator distinct from the one stored in the state.
Equations
Instances For
def
Random.randBound
{m : Type u → Type u_1}
{g : Type}
(α : Type u)
[Preorder α]
[BoundedRandom m α]
(lo hi : α)
(h : lo ≤ hi)
[RandomGen g]
:
Generate a random value of type α between x and y inclusive.
Equations
- Random.randBound α lo hi h = BoundedRandom.randomR lo hi h
Instances For
Equations
- Random.instBool = { random := fun {g : Type} [RandomGen g] => Random.randBool }
instance
Random.instULiftOfULiftable
{m : Type u → Type u_1}
{m' : Type (max v u) → Type u_2}
{α : Type u}
[ULiftable m m']
[Random m α]
:
Random m' (ULift.{v, u} α)
Equations
- Random.instULiftOfULiftable = { random := fun {g : Type} [RandomGen g] => ULiftable.up Random.random }
Equations
- One or more equations did not get rendered due to their size.
instance
Random.instBoundedRandomFin
{m : Type → Type u_1}
[Monad m]
{n : ℕ}
:
BoundedRandom m (Fin n)
Equations
- One or more equations did not get rendered due to their size.
instance
Random.instBoundedRandomULiftOfULiftableOfMonad
{m : Type u → Type u_1}
{m' : Type (max v u) → Type u_2}
{α : Type u}
[Preorder α]
[ULiftable m m']
[BoundedRandom m α]
[Monad m']
:
BoundedRandom m' (ULift.{v, u} α)
Equations
- One or more equations did not get rendered due to their size.
def
IO.runRand
{m : Type u_1 → Type u_2}
{m₀ : Type → Type}
[Monad m]
[MonadLiftT (ST RealWorld) m₀]
[ULiftable m₀ m]
{α : Type u_1}
(cmd : RandT m α)
:
m α
Execute RandT m α using the global stdGenRef as RNG.
Note that:
stdGenRefis not necessarily properly seeded on program startup as of now and will therefore be deterministic.stdGenRefis 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
def
IO.runRandWith
{m : Type u_1 → Type u_2}
[Monad m]
{α : Type u_1}
(seed : ℕ)
(cmd : RandT m α)
:
m α
Execute RandT m α using the global stdGenRef as RNG and the given seed.
Equations
- IO.runRandWith seed cmd = do let __do_lift ← StateT.run cmd { down := mkStdGen seed } pure __do_lift.1