-- |
-- Module:     FRP.NetWire.Random
-- Copyright:  (c) 2011 Ertugrul Soeylemez
-- License:    BSD3
-- Maintainer: Ertugrul Soeylemez <es@ertes.de>
--
-- Noise generators.

module FRP.NetWire.Random
    ( -- * Noise generators
      noise,
      noise1,
      noiseGen,
      noiseR,
      wackelkontakt
    )
    where

import FRP.NetWire.Wire
import System.Random.Mersenne


-- | Noise between 0 (inclusive) and 1 (exclusive).

noise :: Wire a Double
noise = noiseGen


-- | Noise between -1 and 1 exclusive.

noise1 :: Wire a Double
noise1 =
    mkGen $ \(wsRndGen -> mt) _ -> do
        x <- fmap (pred . (2*)) $ random mt
        x `seq` return (Just x, noise1)


-- | Noise.

noiseGen :: MTRandom b => Wire a b
noiseGen =
    mkGen $ \(wsRndGen -> mt) _ -> do
        x <- random mt
        x `seq` return (Just x, noiseGen)


-- | Noise between 0 (inclusive) and the input signal (exclusive).

noiseR :: (Real a, Integral b) => Wire a b
noiseR =
    mkGen $ \(wsRndGen -> mt) n -> do
        x' <- random mt
        let x = floor ((x' :: Double) * realToFrac n)
        x `seq` return (Just x, noiseR)


-- | Random boolean.

wackelkontakt :: Wire a Bool
wackelkontakt = noiseGen