An opaque type modeling a "random variable" - a value which depends on the outcome of some random event. RVars can be conveniently defined by an imperative-looking style:
normalPair = do
u <- stdUniform
t <- stdUniform
let r = sqrt (-2 * log u)
theta = (2 * pi) * t
x = r * cos theta
y = r * sin theta
return (x,y)OR by a more applicative style:
logNormal = exp <$> stdNormalOnce defined (in any style), there are several ways to sample RVars:
Using an immutable pseudo-random number generator that has an instance for RandomGen with StateT monad:
import qualified Data.Random as Fu (uniform)import System.Random (mkStdGen)import Control.Monad.State (runState)runState (sampleStateRVar (Fu.uniform 1 (100 :: Integer))) (mkStdGen 2021)(79,StdGen {unStdGen = SMGen 4687568268719557181 4805600293067301895})
Using a mutable pseud-random number generator that has an instance for StatefulGen with ReaderT monad.
import qualified Data.Random as Fu (uniform)import System.Random.MWC (create)import Control.Monad.Reader (runReaderT)import qualified Data.Vector.Storable as VSinitialize (VS.singleton 2021) >>= runReaderT (sampleReaderRVar (uniform 1 (100 :: Integer)))8