module MEP.Random
(
draw
, getNormal
, getMaxInt
, withProbability
, getBool, getInt, getWord, getDouble
, runRandom, evalRandom
, Rand, Random
) where
import Control.Monad.Mersenne.Random
import Data.Complex (Complex (..))
import System.Random
import Data.Vector as V
draw :: Vector a -> Rand a
draw xs =
Rand $ \g -> let (n, g') = randomR (0, V.length xs - 1) g
r = xs V.! n
in R r g'
withProbability
:: Double
-> (a -> Rand a)
-> (a -> Rand a)
withProbability p modify x = do
t <- getDouble
if t < p
then modify x
else return x
getMaxInt :: Int
-> Rand Int
getMaxInt n = do
r <- getDouble
return $ floor (r * fromIntegral n)
getNormal :: Rand Double
getNormal = do
u <- getDouble
v <- getDouble
let (c :+ s) = exp (0 :+ (2*pi*v))
let r = sqrt $ (-2) * log u
return $ r*c