{-# language TypeSynonymInstances, FlexibleInstances #-}
module Autolib.Util.RandoM
( module Autolib.Util.RandoM
, Random (..)
)
where
import System.Random ( StdGen, randomR, Random, getStdGen, mkStdGen )
import qualified System.Random
import Control.Monad.State
type RandoM = State StdGen
class Monad m => RandomC m where
randomRIO :: Random a => (a,a) -> m a
instance RandomC IO where
randomRIO :: forall a. Random a => (a, a) -> IO a
randomRIO = (a, a) -> IO a
forall a (m :: * -> *). (Random a, MonadIO m) => (a, a) -> m a
System.Random.randomRIO
instance RandomC RandoM where
randomRIO :: forall a. Random a => (a, a) -> RandoM a
randomRIO (a, a)
bnd = do
g0 <- StateT StdGen Identity StdGen
forall s (m :: * -> *). MonadState s m => m s
get
let ( x, g1 ) = randomR bnd g0
put g1
return x
randomly :: Int -> RandoM a -> a
randomly :: forall a. Int -> RandoM a -> a
randomly Int
seed RandoM a
action = RandoM a -> StdGen -> a
forall s a. State s a -> s -> a
evalState RandoM a
action ( Int -> StdGen
mkStdGen Int
seed )
lift :: RandoM a -> IO a
lift :: forall a. RandoM a -> IO a
lift RandoM a
action = do
g <- IO StdGen
forall (m :: * -> *). MonadIO m => m StdGen
getStdGen
return $ evalState action g