Skip to content

Instantly share code, notes, and snippets.

@myyc
Created March 14, 2014 09:16
Show Gist options
  • Save myyc/9544532 to your computer and use it in GitHub Desktop.
Save myyc/9544532 to your computer and use it in GitHub Desktop.
module RndCrap where
import System.Random
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
boxm :: Float -> Float -> (Float, Float)
boxm u v = (r*cos(t), r*(sin(t)))
where r = sqrt (-2 * log u)
t = 2*pi*v
normals :: StdGen -> [Float]
normals g = [fst b, snd b] ++ (normals . wind . wind) g
where b = boxm (a !! 0) (a !! 1)
a = take 2 (randoms g :: [Float])
wind = snd . next
-- discrete brownian motion
bm :: StdGen -> [Float]
bm g = 0 : zipWith (+) (bm g) (normals g)
-- geometric brownian motion (\mu = 1/2)
gbm :: StdGen -> [Float]
gbm g = map (\w -> exp (0.2*w)) (bm g)
(+++) :: T.Text -> T.Text -> T.Text
a +++ b = T.concat [a,b]
main :: IO ()
main = getStdGen >>= \g -> TIO.putStrLn $ l +++ (T.tail . T.init) (arr g) +++ r
where arr g = T.pack . show $ take 40 (gbm g)
l = T.pack "c("
r = T.pack ")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment