Copyright | Copyright (c) 2014, Christopher Chalmers <c.chalmers@me.com> |
---|---|
License | BSD3 |
Maintainer | Christopher Chalmers <c.chalmers@me.com> |
Stability | experimental |
Portability | CPP, FFI |
Safe Haskell | None |
Language | Haskell2010 |
Unique variant of the PCG random number generator. Guarantees the sequence to be unique by using the pointer address to select the output sequence.
There is no way to freeze the state because then it wouldn't be unique anymore. Also, generators can't be initialized in ST because we don't know what pointer reference they'll get.
See http://www.pcg-random.org for details.
import System.Random.PCG.Unique three :: IO [Double] three = do g <- create a <- uniform g b <- uniform g c <- uniform g return [a,b,c]
- data Gen
- create :: IO Gen
- createSystemRandom :: IO Gen
- initialize :: Word64 -> IO Gen
- withSystemRandom :: (Gen -> IO a) -> IO a
- class Variate a where
- advance :: Word64 -> Gen -> IO ()
- retract :: Word64 -> Gen -> IO ()
- uniformW8 :: Generator g m => g -> m Word8
- uniformW16 :: Generator g m => g -> m Word16
- uniformW32 :: Generator g m => g -> m Word32
- uniformW64 :: Generator g m => g -> m Word64
- uniformI8 :: Generator g m => g -> m Int8
- uniformI16 :: Generator g m => g -> m Int16
- uniformI32 :: Generator g m => g -> m Int32
- uniformI64 :: Generator g m => g -> m Int64
- uniformF :: Generator g m => g -> m Float
- uniformD :: Generator g m => g -> m Double
- uniformBool :: Generator g m => g -> m Bool
Gen
createSystemRandom :: IO Gen Source
Seed a PRNG with data from the system's fast source of pseudo-random
numbers. All the caveats of withSystemRandom
apply here as well.
initialize :: Word64 -> IO Gen Source
Create a generator from two words. Note: this is not the same as the
two words in a Seed
.
withSystemRandom :: (Gen -> IO a) -> IO a Source
Seed with system random number. ("/dev/urandom
" on Unix-like
systems, time otherwise).
Getting random numbers
uniform :: Generator g m => g -> m a Source
Generate a uniformly distributed random vairate.
- Use entire range for integral types.
- Use (0,1] range for floating types.
uniformR :: Generator g m => (a, a) -> g -> m a Source
Generate a uniformly distributed random vairate in the given range.
- Use inclusive range for integral types.
- Use (a,b] range for floating types.
Variate Bool | |
Variate Double | |
Variate Float | |
Variate Int | |
Variate Int8 | |
Variate Int16 | |
Variate Int32 | |
Variate Int64 | |
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) |
retract :: Word64 -> Gen -> IO () Source
Retract the given generator n steps in log(2^64-n) time. This
is just advance (-n)
.
Type restricted versions
uniformW16 :: Generator g m => g -> m Word16 Source
uniformW32 :: Generator g m => g -> m Word32 Source
uniformW64 :: Generator g m => g -> m Word64 Source
uniformI16 :: Generator g m => g -> m Int16 Source
uniformI32 :: Generator g m => g -> m Int32 Source
uniformI64 :: Generator g m => g -> m Int64 Source
uniformBool :: Generator g m => g -> m Bool Source