The areas / V33. Data structures and computation
Mathlib.Control.Random
29 declarations. Mathlib docs · Source
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
def Random.ctorIdx E1 #
{m : Type u → Type u_1} → {α : Type u} → Random m α → ℕ
def BoundedRandom.ctorIdx E1 #
{m : Type u → Type u_1} → {α : Type u} → {inst : Preorder α} → BoundedRandom m α → ℕ
abbrev RandGT E3 #
Type → (Type u_1 → Type u_2) → Type u_1 → Type (max u_1 u_2)
A monad transformer to generate random objects using the generic generator type g
abbrev RandG E3 #
Type → Type u_1 → Type u_1
A monad to generate random objects using the generator type g.
abbrev RandT E3 #
(Type u_1 → Type u_2) → Type u_1 → Type (max u_1 u_2)
A monad transformer to generate random objects using the generator type StdGen.
RandT m α should be thought of a random value in m α.
abbrev Rand E3 #
Type u_1 → Type u_1
A monad to generate random objects using the generator type StdGen.
instance instMonadLiftTRandGTOfMonadLift E0 #
{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)
class Random E3 #
(Type u → Type u_1) → Type u → Type (max (max 1 u) u_1)
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.
ctor Random.mk E0 #
{m : Type u → Type u_1} → {α : Type u} → ({g : Type} → [RandomGen g] → RandGT g m α) → Random m α
abbrev Random.random E3 #
{m : Type u → Type u_1} → {α : Type u} → [self : Random m α] → {g : Type} → [RandomGen g] → RandGT g m α
Sample an element of this type from the provided generator.
class BoundedRandom E3 #
(Type u → Type u_1) → (α : Type u) → [Preorder α] → Type (max (max 1 u) u_1)
BoundedRandom m α gives us machinery to generate values of type α between certain bounds in
the monad m.
ctor BoundedRandom.mk E0 #
{m : Type u → Type u_1} →
{α : Type u} →
[inst : Preorder α] →
({g : Type} → (lo hi : α) → lo ≤ hi → [RandomGen g] → RandGT g m { a // lo ≤ a ∧ a ≤ hi }) → BoundedRandom m α
abbrev BoundedRandom.randomR E3 #
{m : Type u → Type u_1} →
{α : Type u} →
{inst : Preorder α} →
[self : BoundedRandom m α] →
{g : Type} → (lo hi : α) → lo ≤ hi → [RandomGen g] → RandGT g m { a // lo ≤ a ∧ a ≤ hi }
Sample a bounded element of this type from the provided generator.
def Rand.next E1 #
{g : Type} → {m : Type → Type u_1} → [RandomGen g] → [Monad m] → RandGT g m ℕ
Generate a random Nat.
def Rand.split E1 #
{m : Type → Type u_1} → {g : Type} → [RandomGen g] → [Monad m] → RandGT g m g
Create a new random number generator distinct from the one stored in the state.
def Rand.range E1 #
{m : Type → Type u_1} → {g : Type} → [RandomGen g] → [Monad m] → RandGT g m (ℕ × ℕ)
Get the range of Nat that can be generated by the generator g.
def Random.rand E1 #
{m : Type u → Type u_1} → {g : Type} → (α : Type u) → [Random m α] → [RandomGen g] → RandGT g m α
Generate a random value of type α.
def Random.randBound E1 #
{m : Type u → Type u_1} →
{g : Type} →
(α : Type u) →
[inst : Preorder α] →
[BoundedRandom m α] → (lo hi : α) → lo ≤ hi → [RandomGen g] → RandGT g m { a // lo ≤ a ∧ a ≤ hi }
Generate a random value of type α between x and y inclusive.
def Random.randFin E1 #
{m : Type → Type u_1} → [Monad m] → {g : Type} → {n : ℕ} → [NeZero n] → [RandomGen g] → RandGT g m (Fin n)
Generate a random Fin.
instance Random.instFinOfNeZeroNat E0 #
{m : Type → Type u_1} → [Monad m] → {n : ℕ} → [NeZero n] → Random m (Fin n)
def Random.randBool E1 #
{m : Type → Type u_1} → [Monad m] → {g : Type} → [RandomGen g] → RandGT g m Bool
Generate a random Bool.
instance Random.instBool E0 #
{m : Type → Type u_1} → [Monad m] → Random m Bool
instance Random.instULiftOfULiftable E0 #
{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} α)
instance Random.instBoundedRandomNat E0 #
{m : Type → Type u_1} → [Monad m] → BoundedRandom m ℕ
instance Random.instBoundedRandomInt E0 #
{m : Type → Type u_1} → [Monad m] → BoundedRandom m ℤ
instance Random.instBoundedRandomFin E0 #
{m : Type → Type u_1} → [Monad m] → {n : ℕ} → BoundedRandom m (Fin n)
instance Random.instBoundedRandomULiftOfULiftableOfMonad E0 #
{m : Type u → Type u_1} →
{m' : Type (max v u) → Type u_2} →
{α : Type u} →
[inst : Preorder α] → [ULiftable m m'] → [BoundedRandom m α] → [Monad m'] → BoundedRandom m' (ULift.{v, u} α)
def IO.runRand E3 #
{m : Type u_1 → Type u_2} →
{m₀ : Type → Type} →
[Monad m] → [MonadLiftT (ST IO.RealWorld) m₀] → [ULiftable m₀ m] → {α : Type u_1} → 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.
def IO.runRandWith E1 #
{m : Type u_1 → Type u_2} → [Monad m] → {α : Type u_1} → ℕ → RandT m α → m α
Execute RandT m α using the global stdGenRef as RNG and the given seed.