Skip to content

Commit

Permalink
Documentation fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbm committed Apr 9, 2011
1 parent 84b22c2 commit 50eed9d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 55 deletions.
91 changes: 50 additions & 41 deletions Data/Random/Normal.hs
Expand Up @@ -6,59 +6,65 @@
Stability : Stable Stability : Stable
Portability: Haskell 98 Portability: Haskell 98
Zero-dependency normally distributed random numbers.
This purpose of this library is to have a simple API and no This purpose of this library is to have a simple API and no
dependencies beyond Haskell 98 in order to let you produce normally dependencies beyond Haskell 98 in order to let you produce normally
distributed random values as soon as possible withe a minimum of distributed random values with a minimum of fuss. This library does
fuss. This library does not attempt to be blazingly fast nor to /not/ attempt to be blazingly fast nor to pass stringent tests of
pass stringent tests of randomness. It attempts to be very easy to randomness. It attempts to be very easy to install and use while
use while being good enough for many applications (simulations, being \"good enough\" for many applications (simulations, games, etc.).
games...). The API is largely analogous to a subset of "System.Random". The API builds upon and is largely analogous to that of the Haskell
Example use: 98 @Random@ module (more recently @System.Random@).
Pure: Pure:
> (sample,g) = normal randomGen -- using a 'System.Random.RandomGen' > (sample,g) = normal myRandomGen -- using a Random.RandomGen
> samples = normals randomGen -- infinite list > samples = normals myRandomGen -- infinite list
> samples2 = mkNormals 108314 -- infinite list using a seed > samples2 = mkNormals 10831452 -- infinite list using a seed
In the IO monad:
With custom mean and standard deviation, e.g.: > sample <- normalIO
> samples <- normalsIO -- infinite list
> sample' = normal' (mean,sigma) stdGen With custom mean and standard deviation:
> samples' = normals' (mean,sigma) stdGen
In the IO monad: > sample = normal' (mean,sigma) myRandomGen
> samples = normals' (mean,sigma) myRandomGen
> samples2 = mkNormals' (mean,sigma) 10831452
> sample <- ioNormal > sample <- normalIO' (mean,sigma)
> samples <- ioNormals > samples <- normalsIO' (mean,sigma)
> sample' <- ioNormal' (mean,sigma)
> samples' <- ioNormals' (mean,sigma)
The library builds upon "System.Random" and uses the Central Limit Internally the library uses the Central Limit Theorem to approximate
Theorem to approximate normally distributed values from multiple normally distributed values from multiple uniformly distributed
uniformly distributed random values. random values.
-} -}




module Data.Random.Normal module Data.Random.Normal (
( normal -- * Pure interface
normal
, normals , normals
, mkNormals , mkNormals
, normalIO
, normalsIO


-- ** Custom mean and standard deviation
, normal' , normal'
, normals' , normals'
, mkNormals' , mkNormals'

-- * Using the global random number generator
, normalIO
, normalsIO

-- ** Custom mean and standard deviation
, normalIO' , normalIO'
, normalsIO' , normalsIO'


) where ) where


import Data.List (mapAccumL) import List (mapAccumL) -- Data.List
import System.Random import Random -- System.Random




-- Normal distribution approximation -- Normal distribution approximation
Expand All @@ -73,11 +79,11 @@ centralLimitTheorem ss = sum (take 12 ss) - 6




-- API -- API
-- --- -- ===
-- | Takes a random number generator g, and returns a random value -- | Takes a random number generator g, and returns a random value
-- normally distributed with mean 0 and standard deviation 1, -- normally distributed with mean 0 and standard deviation 1,
-- together with a new generator. This function is ananalogous to -- together with a new generator. This function is ananalogous to
-- 'System.Random.random'. -- 'Random.random'.
normal :: (RandomGen g, Random a, Fractional a) => g -> (a,g) normal :: (RandomGen g, Random a, Fractional a) => g -> (a,g)
normal g = (centralLimitTheorem as, g') normal g = (centralLimitTheorem as, g')
-- While The Haskell 98 report says "For fractional types, the -- While The Haskell 98 report says "For fractional types, the
Expand All @@ -93,46 +99,49 @@ normals g = x:normals g' where (x,g') = normal g


-- | Creates a infinite list of normally distributed random values -- | Creates a infinite list of normally distributed random values
-- from the provided random generator seed. (In the implementation -- from the provided random generator seed. (In the implementation
-- the seed is fed to 'Data.Random.mkStdGen' to produce the random -- the seed is fed to 'Random.mkStdGen' to produce the random
-- number generator.) -- number generator.)
mkNormals :: (Random a, Fractional a) => Int -> [a] mkNormals :: (Random a, Fractional a) => Int -> [a]
mkNormals = normals . mkStdGen mkNormals = normals . mkStdGen



-- | A variant of 'normal' that uses the global random number -- | A variant of 'normal' that uses the global random number
-- generator. -- generator.
normalIO :: (Random a, Fractional a) => IO a normalIO :: (Random a, Fractional a) => IO a
normalIO = fmap centralLimitTheorem $ mapM randomRIO $ repeat (0,1) normalIO = fmap centralLimitTheorem $ mapM randomRIO $ repeat (0,1)


-- | Creates a infinite list of normally distributed random values -- | Creates a infinite list of normally distributed random values
-- using the global random number generator. (In the implementation -- using the global random number generator. (In the implementation
-- 'System.Random.newStdGen' is used.) -- 'Random.newStdGen' is used.)
normalsIO :: (Random a, Fractional a) => IO [a] normalsIO :: (Random a, Fractional a) => IO [a]
normalsIO = fmap normals newStdGen normalsIO = fmap normals newStdGen




-- With mean and standard deviation -- With mean and standard deviation
-- -------------------------------- -- --------------------------------
-- | Analogous to 'normal' but uses the provided mean and standard -- | Analogous to 'normal' but uses the supplied (mean, standard
-- deviation. -- deviation).
normal' :: (RandomGen g, Random a, Fractional a) => (a,a) -> g -> (a,g) normal' :: (RandomGen g, Random a, Fractional a) => (a,a) -> g -> (a,g)
normal' (mean, sigma) g = (x * sigma + mean, g') where (x, g') = normal g normal' (mean, sigma) g = (x * sigma + mean, g') where (x, g') = normal g


-- | Analogous to 'normals' but uses the provided mean and standard -- | Analogous to 'normals' but uses the supplied (mean, standard
-- deviation. -- deviation).
normals' :: (RandomGen g, Random a, Fractional a) => (a,a) -> g -> [a] normals' :: (RandomGen g, Random a, Fractional a) => (a,a) -> g -> [a]
normals' (mean, sigma) g = map (\x -> x * sigma + mean) (normals g) normals' (mean, sigma) g = map (\x -> x * sigma + mean) (normals g)


-- | Analogous to 'mkNormals' but uses the provided mean and standard -- | Analogous to 'mkNormals' but uses the supplied (mean, standard
-- deviation. -- deviation).
mkNormals' :: (Random a, Fractional a) => (a,a) -> Int -> [a] mkNormals' :: (Random a, Fractional a) => (a,a) -> Int -> [a]
mkNormals' ms= normals' ms . mkStdGen mkNormals' ms= normals' ms . mkStdGen



-- | Analogous to 'normalIO' but uses the supplied (mean, standard
-- deviation).
normalIO' ::(Random a, Fractional a) => (a,a) -> IO a normalIO' ::(Random a, Fractional a) => (a,a) -> IO a
normalIO' (mean,sigma) = fmap (\x -> x * sigma + mean) normalIO normalIO' (mean,sigma) = fmap (\x -> x * sigma + mean) normalIO


-- | Creates a infinite list of normally distributed random values -- | Analogous to 'normalsIO' but uses the supplied (mean, standard
-- using the global random number generator. (In the implementation -- deviation).
-- 'System.Random.newStdGen' is used.)
normalsIO' :: (Random a, Fractional a) => (a,a) -> IO [a] normalsIO' :: (Random a, Fractional a) => (a,a) -> IO [a]
normalsIO' ms = fmap (normals' ms) newStdGen normalsIO' ms = fmap (normals' ms) newStdGen


Expand Down
18 changes: 4 additions & 14 deletions normaldistribution.cabal
Expand Up @@ -5,29 +5,19 @@ License-File: LICENSE
Copyright: Bjorn Buckwalter 2011 Copyright: Bjorn Buckwalter 2011
Author: Bjorn Buckwalter Author: Bjorn Buckwalter
Maintainer: bjorn.buckwalter@gmail.com Maintainer: bjorn.buckwalter@gmail.com
Stability: stable Stability: Stable
--Homepage: https://github.com/bjornbm/normaldistribution --Homepage: https://github.com/bjornbm/normaldistribution
Synopsis: Synopsis:


Zero-dependency (haskell98) normally distributed random values. Minimum fuss normally distributed random values.


Description: Description:


Dimensional-vectors provides basic linear algebra functions for ddddd
vectors and matrices where the elements are physical quantities
from the Dimensional library. Matrices and vectors can be
heterogeneous in the physical dimensions of their elements,
which while mathematically questionable is useful e.g. for
representing state vectors and covariance matrices. All
vector/matrix operations are statically guaranteed to be correct
w.r.t. the vector/matrix dimensions as well as the physical
dimensions of the elements.
The internal vector/matrix representation is slow but should
be readily exchangable for a faster one (e.g. GSLHaskell).


Category: Math Category: Math
Build-Type: Simple Build-Type: Simple
Build-Depends: base < 5 Build-Depends: base < 5, haskell98 < 1.1
Exposed-Modules: Data.Random.Normal Exposed-Modules: Data.Random.Normal
Extra-source-files: README, LICENSE Extra-source-files: README, LICENSE


0 comments on commit 50eed9d

Please sign in to comment.