statistics-0.3.5: A library of statistical types, data, and functions

Portability portable experimental bos@serpentine.com

Statistics.RandomVariate

Description

Pseudo-random variate generation.

Synopsis

# Types

data Gen s Source

State of the pseudo-random number generator.

data Seed Source

An immutable snapshot of the state of a `Gen`.

Instances

 Eq Seed Read Seed Show Seed Typeable Seed

class Variate a whereSource

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.

Note: Marsaglia's PRNG is not known to be cryptographically secure, so you should not use it for cryptographic operations.

Methods

uniform :: Gen s -> ST s aSource

Generate a single uniformly distributed random variate. The range of values produced varies by type:

• For fixed-width integral types, the type's entire range is used.
• 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 `log` function).
• The range of random `Integer` variates is the same as for `Int`.

To generate a `Float` variate with a range of [0,1), subtract 2**(-33). To do the same with `Double` variates, subtract 2**(-53).

Instances

 Variate Bool Variate Double Variate Float Variate Int Variate Int8 Variate Int16 Variate Int32 Variate Int64 Variate Integer Variate Word Variate Word8 Variate Word16 Variate Word32 Variate Word64 (Variate a, Variate b) => Variate (a, b) (Variate a, Variate b, Variate c) => Variate (a, b, c) (Variate a, Variate b, Variate c, Variate d) => Variate (a, b, c, d)

# Other distributions

normal :: Gen s -> ST s DoubleSource

Generate a normally distributed random variate.

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.

# Creation

create :: ST s (Gen s)Source

Create a generator for variates using a fixed seed.

initialize :: UArr Word32 -> ST s (Gen s)Source

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:

``` initialize (singletonU 42)
```
``` initialize (toU [4, 8, 15, 16, 23, 42])
```

If a seed contains fewer than 256 elements, it is first used verbatim, then its elements are `xor`ed against elements of the default seed until 256 elements are reached.

withSystemRandom :: (forall s. Gen s -> ST s a) -> IO aSource

Seed a PRNG with data from the system's fast source of pseudo-random numbers ("/dev/urandom" on Unix-like systems), then run the given action.

Note: on Windows, this code does not yet use the native Cryptographic API as a source of random numbers (it uses the system clock instead). As a result, the sequences it generates may not be highly independent.

# State management

save :: Gen s -> ST s SeedSource

Save the state of a `Gen`, for later use by `restore`.

restore :: Seed -> ST s (Gen s)Source

Create a new `Gen` that mirrors the state of a saved `Seed`.

# Helper functions

uniformArray :: (UA a, Variate a) => Gen s -> Int -> ST s (UArr a)Source

Generate an array of pseudo-random variates. This is not necessarily faster than invoking `uniform` repeatedly in a loop, but it may be more convenient to use in some situations.