-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Fast, high quality pseudo random number generation
--   
--   This package contains code for generating high quality random numbers
--   that follow either a uniform or normal distribution. The generated
--   numbers are suitable for use in statistical applications.
--   
--   The uniform PRNG uses Marsaglia's MWC256 (also known as MWC8222)
--   multiply-with-carry generator, which has a period of 2^8222 and fares
--   well in tests of randomness. It is also extremely fast, between 2 and
--   3 times faster than the Mersenne Twister.
--   
--   Compared to the mersenne-random package, this package has a more
--   convenient API, is faster, and supports more statistical
--   distributions.
@package mwc-random
@version 0.13.5.0


-- | Pseudo-random number generation. This module contains code for
--   generating high quality random numbers that follow a uniform
--   distribution.
--   
--   For non-uniform distributions, see the <a>Distributions</a> module.
--   
--   The uniform PRNG uses Marsaglia's MWC256 (also known as MWC8222)
--   multiply-with-carry generator, which has a period of 2^8222 and fares
--   well in tests of randomness. It is also extremely fast, between 2 and
--   3 times faster than the Mersenne Twister.
--   
--   The generator state is stored in the <a>Gen</a> data type. It can be
--   created in several ways:
--   
--   <ol>
--   <li>Using the <a>withSystemRandom</a> call, which creates a random
--   state.</li>
--   <li>Supply your own seed to <a>initialize</a> function.</li>
--   <li>Finally, <a>create</a> makes a generator from a fixed seed.
--   Generators created in this way aren't really random.</li>
--   </ol>
--   
--   For repeatability, the state of the generator can be snapshotted and
--   replayed using the <a>save</a> and <a>restore</a> functions.
--   
--   The simplest use is to generate a vector of uniformly distributed
--   values:
--   
--   <pre>
--   vs &lt;- <a>withSystemRandom</a> . <a>asGenST</a> $ \gen -&gt; <a>uniformVector</a> gen 100
--   </pre>
--   
--   These values can be of any type which is an instance of the class
--   <a>Variate</a>.
--   
--   To generate random values on demand, first <a>create</a> a random
--   number generator.
--   
--   <pre>
--   gen &lt;- <a>create</a>
--   </pre>
--   
--   Hold onto this generator and use it wherever random values are
--   required (creating a new generator is expensive compared to generating
--   a random number, so you don't want to throw them away). Get a random
--   value using <a>uniform</a> or <a>uniformR</a>:
--   
--   <pre>
--   v &lt;- <a>uniform</a> gen
--   </pre>
--   
--   <pre>
--   v &lt;- <a>uniformR</a> (1, 52) gen
--   </pre>
module System.Random.MWC

-- | State of the pseudo-random number generator. It uses mutable state so
--   same generator shouldn't be used from the different threads
--   simultaneously.
data Gen s

-- | Create a generator for variates using a fixed seed.
create :: PrimMonad m => m (Gen (PrimState m))

-- | Create a generator for variates using the given seed, of which up to
--   256 elements will be used. For arrays of less than 256 elements, part
--   of the default seed will be used to finish initializing the
--   generator's state.
--   
--   Examples:
--   
--   <pre>
--   initialize (singleton 42)
--   </pre>
--   
--   <pre>
--   initialize (fromList [4, 8, 15, 16, 23, 42])
--   </pre>
--   
--   If a seed contains fewer than 256 elements, it is first used verbatim,
--   then its elements are <a>xor</a>ed against elements of the default
--   seed until 256 elements are reached.
--   
--   If a seed contains exactly 258 elements, then the last two elements
--   are used to set the generator's initial state. This allows for
--   complete generator reproducibility, so that e.g. <tt>gen' == gen</tt>
--   in the following example:
--   
--   <pre>
--   gen' &lt;- <a>initialize</a> . <a>fromSeed</a> =&lt;&lt; <a>save</a>
--   </pre>
initialize :: (PrimMonad m, Vector v Word32) => v Word32 -> m (Gen (PrimState m))

-- | Seed a PRNG with data from the system's fast source of pseudo-random
--   numbers ("<tt>/dev/urandom</tt>" on Unix-like systems or
--   <tt>RtlGenRandom</tt> on Windows), then run the given action.
--   
--   This is a somewhat expensive function, and is intended to be called
--   only occasionally (e.g. once per thread). You should use the
--   <a>Gen</a> it creates to generate many random numbers.
withSystemRandom :: PrimBase m => (Gen (PrimState m) -> m a) -> IO a

-- | Seed a PRNG with data from the system's fast source of pseudo-random
--   numbers. All the caveats of <a>withSystemRandom</a> apply here as
--   well.
createSystemRandom :: IO GenIO

-- | A shorter name for PRNG state in the <a>IO</a> monad.
type GenIO = Gen (PrimState IO)

-- | A shorter name for PRNG state in the <a>ST</a> monad.
type GenST s = Gen (PrimState (ST s))

-- | Constrain the type of an action to run in the <a>IO</a> monad.
asGenIO :: (GenIO -> IO a) -> (GenIO -> IO a)

-- | Constrain the type of an action to run in the <a>ST</a> monad.
asGenST :: (GenST s -> ST s a) -> (GenST s -> ST s a)

-- | The class of types for which we can generate uniformly distributed
--   random variates.
--   
--   The uniform PRNG uses Marsaglia's MWC256 (also known as MWC8222)
--   multiply-with-carry generator, which has a period of 2^8222 and fares
--   well in tests of randomness. It is also extremely fast, between 2 and
--   3 times faster than the Mersenne Twister.
--   
--   <i>Note</i>: Marsaglia's PRNG is not known to be cryptographically
--   secure, so you should not use it for cryptographic operations.
class Variate a

-- | Generate a single uniformly distributed random variate. The range of
--   values produced varies by type:
--   
--   <ul>
--   <li>For fixed-width integral types, the type's entire range is
--   used.</li>
--   <li>For floating point numbers, the range (0,1] is used. Zero is
--   explicitly excluded, to allow variates to be used in statistical
--   calculations that require non-zero values (e.g. uses of the <a>log</a>
--   function).</li>
--   </ul>
--   
--   To generate a <a>Float</a> variate with a range of [0,1), subtract
--   2**(-33). To do the same with <a>Double</a> variates, subtract
--   2**(-53).
uniform :: (Variate a, PrimMonad m) => Gen (PrimState m) -> m a

-- | Generate single uniformly distributed random variable in a given
--   range.
--   
--   <ul>
--   <li>For integral types inclusive range is used.</li>
--   <li>For floating point numbers range (a,b] is used if one ignores
--   rounding errors.</li>
--   </ul>
uniformR :: (Variate a, PrimMonad m) => (a, a) -> Gen (PrimState m) -> m a

-- | Generate a vector of pseudo-random variates. This is not necessarily
--   faster than invoking <a>uniform</a> repeatedly in a loop, but it may
--   be more convenient to use in some situations.
uniformVector :: (PrimMonad m, Variate a, Vector v a) => Gen (PrimState m) -> Int -> m (v a)

-- | An immutable snapshot of the state of a <a>Gen</a>.
data Seed

-- | Convert seed into vector.
fromSeed :: Seed -> Vector Word32

-- | Convert vector to <a>Seed</a>. It acts similarily to <a>initialize</a>
--   and will accept any vector. If you want to pass seed immediately to
--   restore you better call initialize directly since following law holds:
--   
--   <pre>
--   restore (toSeed v) = initialize v
--   </pre>
toSeed :: (Vector v Word32) => v Word32 -> Seed

-- | Save the state of a <a>Gen</a>, for later use by <a>restore</a>.
save :: PrimMonad m => Gen (PrimState m) -> m Seed

-- | Create a new <a>Gen</a> that mirrors the state of a saved <a>Seed</a>.
restore :: PrimMonad m => Seed -> m (Gen (PrimState m))
instance GHC.Show.Show System.Random.MWC.Seed
instance GHC.Classes.Eq System.Random.MWC.Seed
instance System.Random.MWC.Variate GHC.Int.Int8
instance System.Random.MWC.Variate GHC.Int.Int16
instance System.Random.MWC.Variate GHC.Int.Int32
instance System.Random.MWC.Variate GHC.Int.Int64
instance System.Random.MWC.Variate GHC.Word.Word8
instance System.Random.MWC.Variate GHC.Word.Word16
instance System.Random.MWC.Variate GHC.Word.Word32
instance System.Random.MWC.Variate GHC.Word.Word64
instance System.Random.MWC.Variate GHC.Types.Bool
instance System.Random.MWC.Variate GHC.Types.Float
instance System.Random.MWC.Variate GHC.Types.Double
instance System.Random.MWC.Variate GHC.Types.Int
instance System.Random.MWC.Variate GHC.Types.Word
instance (System.Random.MWC.Variate a, System.Random.MWC.Variate b) => System.Random.MWC.Variate (a, b)
instance (System.Random.MWC.Variate a, System.Random.MWC.Variate b, System.Random.MWC.Variate c) => System.Random.MWC.Variate (a, b, c)
instance (System.Random.MWC.Variate a, System.Random.MWC.Variate b, System.Random.MWC.Variate c, System.Random.MWC.Variate d) => System.Random.MWC.Variate (a, b, c, d)


-- | Pseudo-random number generation for non-uniform distributions.
module System.Random.MWC.Distributions

-- | Generate a normally distributed random variate with given mean and
--   standard deviation.
normal :: PrimMonad m => Double -> Double -> Gen (PrimState m) -> m Double

-- | Generate a normally distributed random variate with zero mean and unit
--   variance.
--   
--   The implementation uses Doornik's modified ziggurat algorithm.
--   Compared to the ziggurat algorithm usually used, this is slower, but
--   generates more independent variates that pass stringent tests of
--   randomness.
standard :: PrimMonad m => Gen (PrimState m) -> m Double

-- | Generate an exponentially distributed random variate.
exponential :: PrimMonad m => Double -> Gen (PrimState m) -> m Double

-- | Generate truncated exponentially distributed random variate.
truncatedExp :: PrimMonad m => Double -> (Double, Double) -> Gen (PrimState m) -> m Double

-- | Random variate generator for gamma distribution.
gamma :: PrimMonad m => Double -> Double -> Gen (PrimState m) -> m Double

-- | Random variate generator for the chi square distribution.
chiSquare :: PrimMonad m => Int -> Gen (PrimState m) -> m Double

-- | Random variate generator for Beta distribution
beta :: PrimMonad m => Double -> Double -> Gen (PrimState m) -> m Double

-- | Random variate generator for categorical distribution.
--   
--   Note that if you need to generate a lot of variates functions
--   <a>System.Random.MWC.CondensedTable</a> will offer better performance.
--   If only few is needed this function will faster since it avoids costs
--   of setting up table.
categorical :: (PrimMonad m, Vector v Double) => v Double -> Gen (PrimState m) -> m Int

-- | Random variate generator for categorical distribution where the
--   weights are in the log domain. It's implemented in terms of
--   <a>categorical</a>.
logCategorical :: (PrimMonad m, Vector v Double) => v Double -> Gen (PrimState m) -> m Int

-- | Random variate generator for the geometric distribution, computing the
--   number of failures before success. Distribution's support is [0..].
geometric0 :: PrimMonad m => Double -> Gen (PrimState m) -> m Int

-- | Random variate generator for geometric distribution for number of
--   trials. Distribution's support is [1..] (i.e. just <a>geometric0</a>
--   shifted by 1).
geometric1 :: PrimMonad m => Double -> Gen (PrimState m) -> m Int

-- | Random variate generator for Bernoulli distribution
bernoulli :: PrimMonad m => Double -> Gen (PrimState m) -> m Bool

-- | Random variate generator for Dirichlet distribution
dirichlet :: (PrimMonad m, Traversable t) => t Double -> Gen (PrimState m) -> m (t Double)

-- | Random variate generator for uniformly distributed permutations. It
--   returns random permutation of vector <i>[0 .. n-1]</i>.
--   
--   This is the Fisher-Yates shuffle
uniformPermutation :: forall m v. (PrimMonad m, Vector v Int) => Int -> Gen (PrimState m) -> m (v Int)

-- | Random variate generator for a uniformly distributed shuffle (all
--   shuffles are equiprobable) of a vector. It uses Fisher-Yates shuffle
--   algorithm.
uniformShuffle :: (PrimMonad m, Vector v a) => v a -> Gen (PrimState m) -> m (v a)

-- | In-place uniformly distributed shuffle (all shuffles are
--   equiprobable)of a vector.
uniformShuffleM :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Gen (PrimState m) -> m ()


-- | Table-driven generation of random variates. This approach can generate
--   random variates in <i>O(1)</i> time for the supported distributions,
--   at a modest cost in initialization time.
module System.Random.MWC.CondensedTable

-- | A lookup table for arbitrary discrete distributions. It allows the
--   generation of random variates in <i>O(1)</i>. Note that probability is
--   quantized in units of <tt>1/2^32</tt>, and all distributions with
--   infinite support (e.g. Poisson) should be truncated.
data CondensedTable v a

-- | A <a>CondensedTable</a> that uses boxed vectors, and is able to hold
--   any type of element.
type CondensedTableV = CondensedTable Vector

-- | A <a>CondensedTable</a> that uses unboxed vectors.
type CondensedTableU = CondensedTable Vector

-- | Generate a random value using a condensed table.
genFromTable :: (PrimMonad m, Vector v a) => CondensedTable v a -> Gen (PrimState m) -> m a

-- | Generate a condensed lookup table from a list of outcomes with given
--   probabilities. The vector should be non-empty and the probabilites
--   should be non-negative and sum to 1. If this is not the case, this
--   algorithm will construct a table for some distribution that may bear
--   no resemblance to what you intended.
tableFromProbabilities :: (Vector v (a, Word32), Vector v (a, Double), Vector v a, Vector v Word32) => v (a, Double) -> CondensedTable v a

-- | Same as <a>tableFromProbabilities</a> but treats number as weights not
--   probilities. Non-positive weights are discarded, and those remaining
--   are normalized to 1.
tableFromWeights :: (Vector v (a, Word32), Vector v (a, Double), Vector v a, Vector v Word32) => v (a, Double) -> CondensedTable v a

-- | Generate a condensed lookup table from integer weights. Weights should
--   sum to <tt>2^32</tt> at least approximately. This function will
--   correct small deviations from <tt>2^32</tt> such as arising from
--   rounding errors. But for large deviations it's likely to product
--   incorrect result with terrible performance.
tableFromIntWeights :: (Vector v (a, Word32), Vector v a, Vector v Word32) => v (a, Word32) -> CondensedTable v a

-- | Create a lookup table for the Poisson distibution. Note that table
--   construction may have significant cost. For λ &lt; 100 it takes as
--   much time to build table as generation of 1000-30000 variates.
tablePoisson :: Double -> CondensedTableU Int

-- | Create a lookup table for the binomial distribution.
tableBinomial :: Int -> Double -> CondensedTableU Int
