code, module Randomisable:
{-# LANGUAGE FlexibleInstances, ScopedTypeVariables #-}
module Randomisable (
Randomisable(..),
Rnd.Random,
Rnd.StdGen,
randomValueS,
randomListS,
randomiseValueProbablyS,
getGenS,
splitGenS,
tplSplitGenS2,
tplSplitGenS3,
tplSplitGenS4,
tplSplitGenS5,
tplSplitGenS6,
tplSplitGenS7,
tplSplitGenS8,
tplSplitGenS9,
lPrngSplit,
splitPrng,
tplPrngSplit2,
tplPrngSplit3,
tplPrngSplit4,
tplPrngSplit5,
tplPrngSplit6,
tplPrngSplit7,
tplPrngSplit8,
tplPrngSplit9,
Rnd.newStdGen
) where
import qualified System.Random as Rnd
import qualified Control.Monad.State as S
--import Debug.Trace
trace :: String -> a -> a
trace _ = id
class Randomisable r where
random :: Rnd.StdGen -> r
random prng = S.evalState randomS prng
randomS :: S.State Rnd.StdGen r
randomise :: Rnd.StdGen -> r -> r
randomise prng r = S.evalState (randomiseS r) prng
randomiseS :: r -> S.State Rnd.StdGen r
randomiseS x = return x
randomValueS :: Rnd.Random a => (a, a) -> S.State Rnd.StdGen a
randomValueS range = S.state (Rnd.randomR range)
randomiseValueProbablyS :: Rnd.Random a => Double -> (a, a) -> a -> S.State Rnd.StdGen a
randomiseValueProbablyS ndPrb rng nValue0 =
do
ndDice <- randomValueS (0.0, 1.0)
if ndDice < ndPrb
then (randomValueS rng)
else (return nValue0)
randomListS :: forall a . Randomisable a => Integer -> S.State Rnd.StdGen [a]
randomListS nPopulation = (trace "randomListS tp#1" $ randomListS' nPopulation [])
where
randomListS' :: forall b. Randomisable b => Integer -> [b] -> S.State Rnd.StdGen [b]
randomListS' nPopulation' lgntyp
| nPopulation' > 0 =
do
r1 <- (trace "randomListS' tp#1 " $ randomS)
result <- (trace ("randomListS' tp#2 nPopulation'=" ++ (show nPopulation')) $ (randomListS' (nPopulation' - 1) (r1 : lgntyp)))
(trace "randomListS' tp#3" $ return result)
| otherwise = (trace "randomListS' tp#4" $ return lgntyp)
getGenS :: S.State Rnd.StdGen Rnd.StdGen
getGenS = S.gets id
splitGenS :: S.State Rnd.StdGen (Rnd.StdGen)
splitGenS =
do
prng <- getGenS
return (splitPrng prng)
tplSplitGenS2 :: S.State Rnd.StdGen (Rnd.StdGen, Rnd.StdGen)
tplSplitGenS2 =
do
prng <- getGenS
return (tplPrngSplit2 prng)
tplSplitGenS3 :: S.State Rnd.StdGen (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplSplitGenS3 =
do
prng <- getGenS
return (tplPrngSplit3 prng)
tplSplitGenS4 :: S.State Rnd.StdGen (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplSplitGenS4 =
do
prng <- getGenS
return (tplPrngSplit4 prng)
tplSplitGenS5 :: S.State Rnd.StdGen (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplSplitGenS5 =
do
prng <- getGenS
return (tplPrngSplit5 prng)
tplSplitGenS6 :: S.State Rnd.StdGen (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplSplitGenS6 =
do
prng <- getGenS
return (tplPrngSplit6 prng)
tplSplitGenS7 :: S.State Rnd.StdGen (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplSplitGenS7 =
do
prng <- getGenS
return (tplPrngSplit7 prng)
tplSplitGenS8 :: S.State Rnd.StdGen (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplSplitGenS8 =
do
prng <- getGenS
return (tplPrngSplit8 prng)
tplSplitGenS9 :: S.State Rnd.StdGen (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplSplitGenS9 =
do
prng <- getGenS
return (tplPrngSplit9 prng)
lPrngSplit :: Rnd.StdGen -> Integer -> [Rnd.StdGen]
lPrngSplit prng0 nCount
| nCount > 0 =
let
(prng1, prng2) = Rnd.split prng0
in
prng2 : (lPrngSplit prng1 (nCount - 1))
| otherwise = []
splitPrng :: Rnd.StdGen -> (Rnd.StdGen)
splitPrng prng0 =
let
[prng1] = lPrngSplit prng0 1
in
(prng1)
tplPrngSplit2 :: Rnd.StdGen -> (Rnd.StdGen, Rnd.StdGen)
tplPrngSplit2 prng0 =
let
[prng1, prng2] = lPrngSplit prng0 2
in
(prng1, prng2)
tplPrngSplit3 :: Rnd.StdGen -> (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplPrngSplit3 prng0 =
let
[prng1, prng2, prng3] = lPrngSplit prng0 3
in
(prng1, prng2, prng3)
tplPrngSplit4 :: Rnd.StdGen -> (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplPrngSplit4 prng0 =
let
[prng1, prng2, prng3, prng4] = lPrngSplit prng0 4
in
(prng1, prng2, prng3, prng4)
tplPrngSplit5 :: Rnd.StdGen -> (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplPrngSplit5 prng0 =
let
[prng1, prng2, prng3, prng4, prng5] = lPrngSplit prng0 5
in
(prng1, prng2, prng3, prng4, prng5)
tplPrngSplit6 :: Rnd.StdGen -> (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplPrngSplit6 prng0 =
let
[prng1, prng2, prng3, prng4, prng5, prng6] = lPrngSplit prng0 6
in
(prng1, prng2, prng3, prng4, prng5, prng6)
tplPrngSplit7 :: Rnd.StdGen -> (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplPrngSplit7 prng0 =
let
[prng1, prng2, prng3, prng4, prng5, prng6, prng7] = lPrngSplit prng0 7
in
(prng1, prng2, prng3, prng4, prng5, prng6, prng7)
tplPrngSplit8 :: Rnd.StdGen -> (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplPrngSplit8 prng0 =
let
[prng1, prng2, prng3, prng4, prng5, prng6, prng7, prng8] = lPrngSplit prng0 8
in
(prng1, prng2, prng3, prng4, prng5, prng6, prng7, prng8)
tplPrngSplit9 :: Rnd.StdGen -> (Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen, Rnd.StdGen)
tplPrngSplit9 prng0 =
let
[prng1, prng2, prng3, prng4, prng5, prng6, prng7, prng8, prng9] = lPrngSplit prng0 9
in
(prng1, prng2, prng3, prng4, prng5, prng6, prng7, prng8, prng9)