User Tools

Site Tools


codesnippets:randomvalues

Random values

  • example
    • code, module Main:
      {-# OPTIONS_GHC -fno-warn-orphans #-}
       
      import qualified Randomisable as Rnd
       
      main :: IO ()
      main = do
        prng0 <- Rnd.newStdGen
        print $ processMyData ((Rnd.random prng0) :: MyData)
       
      processMyData :: MyData -> (MyData, MyData, MyData2, ScaleA)
      processMyData (MyData prng0 nd1 nd2 mydt2) = 
          let
              (prng1, prng2, prng3, prng4, prng5) = Rnd.tplPrngSplit5 prng0
          in
              ((MyData prng1 nd1 nd2 mydt2), (MyData prng2 (Rnd.randomise prng3 nd1) nd2 mydt2), ((Rnd.random prng4) :: MyData2), ((Rnd.random prng5) :: ScaleA))
       
      data MyData = MyData Rnd.StdGen Double Double MyData2
          deriving (Show, Read)
       
      data MyData2 = MyData2 Double Double
          deriving (Show, Read)
       
      newtype ScaleA = ScaleA Double
          deriving (Show, Read)
       
      instance Rnd.Randomisable ScaleA where
          randomS = (Rnd.randomValueS (0.0, 2.0)) >>= (\d -> return (ScaleA d))
       
      instance Rnd.Randomisable Double where
          randomS = Rnd.randomValueS (0.0, 1.0)
          randomiseS n = Rnd.randomiseValueProbablyS 0.1 (0.0, 1.0) n
       
      instance Rnd.Randomisable MyData2 where
          randomS = 
              do
                  r1 <- Rnd.randomS
                  r2 <- Rnd.randomS
                  return (MyData2 r1 r2)
       
      instance Rnd.Randomisable MyData where
          randomS = 
              do
                  prng1 <- Rnd.splitGenS
                  r1 <- Rnd.randomS
                  r2 <- Rnd.randomS
                  r3 <- Rnd.randomS
                  return (MyData prng1 r1 r2 r3)
    • 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)
    • executes, with output like:
      (MyData 1547211741 2147483396 0.2483644234579585 0.9836217202419262 (MyData2 0.4909809627910673 0.8713231916723704),MyData 1547251755 2147402014 0.2483644234579585 0.9836217202419262 (MyData2 0.4909809627910673 0.8713231916723704),MyData2 0.6269406052566662 0.2513090689294605,ScaleA 0.11255263338660004)

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
codesnippets/randomvalues.txt · Last modified: by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki