fork download
  1. import System.Random
  2. import Control.Monad.State
  3. import Control.Applicative
  4.  
  5. monteCarlo :: (Random a, RandomGen g)
  6. => Int -> (a,a) -> (a -> Bool) -> g -> Double
  7. monteCarlo count reg cond gen = fromIntegral hits / fromIntegral count
  8. where rands = take count (randomRs reg gen)
  9. hits = length (filter cond rands)
  10.  
  11.  
  12. instance (Random a, Random b, Random c) => Random (a,b,c) where
  13. random = runState (liftA3 (,,) (state random) (state random) (state random))
  14. randomR ((x1,y1,z1), (x2,y2,z2)) gen1 =
  15. let (x, gen2) = randomR (x1, x2) gen1
  16. (y, gen3) = randomR (y1, y2) gen2
  17. (z, gen4) = randomR (z1, z2) gen3
  18. in ((x,y,z), gen4)
  19.  
  20. main = do
  21. gen <- getStdGen
  22. print (monteCarlo 10000 ((0.0,0.0,0.0),(1.0,1.0,1.0)) isTriangle gen)
  23. where isTriangle :: (Double,Double,Double) -> Bool
  24. isTriangle (x,y,z) = x+y>z && y+z>x && z+x>y
  25.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.hs:2:8:
    Could not find module `Control.Monad.State'
    Perhaps you meant
      Control.Monad.ST (from base)
      Control.Monad.ST.Safe (from base)
      Control.Monad.Fix (from base)
    Use -v to see a list of the files searched for.
stdout
Standard output is empty