module GeomAlg.External.Random(randomInts, randomDoubles, normalRandomDoubles) where
randomInts :: Int -> Int -> [Int]
randomInts s1 s2 =
if 1 <= s1 && s1 <= 2147483562 then
if 1 <= s2 && s2 <= 2147483398 then
rands s1 s2
else
error "randomInts: Bad second seed."
else
error "randomInts: Bad first seed."
rands :: Int -> Int -> [Int]
rands s1 s2 = z' : rands s1'' s2''
where z' = if z < 1 then z + 2147483562 else z
z = s1'' s2''
k = s1 `quot` 53668
s1' = 40014 * (s1 k * 53668) k * 12211
s1'' = if s1' < 0 then s1' + 2147483563 else s1'
k' = s2 `quot` 52774
s2' = 40692 * (s2 k' * 52774) k' * 3791
s2'' = if s2' < 0 then s2' + 2147483399 else s2'
randomDoubles :: Int -> Int -> [Double]
randomDoubles s1 s2 = map (\x -> fromIntegral x * 4.6566130638969828e-10) (randomInts s1 s2)
normalRandomDoubles :: Int -> Int -> [Double]
normalRandomDoubles s1 s2 = boxMuller (map (\x->2*x1) (randomDoubles s1 s2))
boxMuller :: [Double] -> [Double]
boxMuller (x1:x2:xs) | r <= 1 = x1*m : x2*m : rest
| otherwise = rest
where r = x1*x1 + x2*x2
m = sqrt(2*log r/r)
rest = boxMuller xs