Copyright | Copyright (c) 2014-2015 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 |
Single variant of the PCG random number generator. This module only uses a single stream. See http://www.pcg-random.org for details.
import Control.Monad.ST import System.Random.PCG.Single three :: [Double] three = runST $ do g <- create a <- uniform g b <- uniform g c <- uniform g return [a,b,c]
Synopsis
- data Gen s
- type GenIO = Gen RealWorld
- type GenST = Gen
- create :: PrimMonad m => m (Gen (PrimState m))
- createSystemRandom :: IO GenIO
- initialize :: PrimMonad m => Word64 -> m (Gen (PrimState m))
- withSystemRandom :: (GenIO -> IO a) -> IO a
- withFrozen :: FrozenGen -> (forall s. Gen s -> ST s a) -> (a, FrozenGen)
- class Variate a where
- advance :: PrimMonad m => Word64 -> Gen (PrimState m) -> m ()
- retract :: PrimMonad m => Word64 -> Gen (PrimState m) -> m ()
- data FrozenGen
- save :: PrimMonad m => Gen (PrimState m) -> m FrozenGen
- restore :: PrimMonad m => FrozenGen -> m (Gen (PrimState m))
- seed :: FrozenGen
- initFrozen :: Word64 -> FrozenGen
- 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
- uniformRW8 :: Generator g m => (Word8, Word8) -> g -> m Word8
- uniformRW16 :: Generator g m => (Word16, Word16) -> g -> m Word16
- uniformRW32 :: Generator g m => (Word32, Word32) -> g -> m Word32
- uniformRW64 :: Generator g m => (Word64, Word64) -> g -> m Word64
- uniformRI8 :: Generator g m => (Int8, Int8) -> g -> m Int8
- uniformRI16 :: Generator g m => (Int16, Int16) -> g -> m Int16
- uniformRI32 :: Generator g m => (Int32, Int32) -> g -> m Int32
- uniformRI64 :: Generator g m => (Int64, Int64) -> g -> m Int64
- uniformRF :: Generator g m => (Float, Float) -> g -> m Float
- uniformRD :: Generator g m => (Double, Double) -> g -> m Double
- uniformRBool :: Generator g m => (Bool, Bool) -> g -> m Bool
- uniformBW8 :: Generator g m => Word8 -> g -> m Word8
- uniformBW16 :: Generator g m => Word16 -> g -> m Word16
- uniformBW32 :: Generator g m => Word32 -> g -> m Word32
- uniformBW64 :: Generator g m => Word64 -> g -> m Word64
- uniformBI8 :: Generator g m => Int8 -> g -> m Int8
- uniformBI16 :: Generator g m => Int16 -> g -> m Int16
- uniformBI32 :: Generator g m => Int32 -> g -> m Int32
- uniformBI64 :: Generator g m => Int64 -> g -> m Int64
- uniformBF :: Generator g m => Float -> g -> m Float
- uniformBD :: Generator g m => Double -> g -> m Double
- uniformBBool :: Generator g m => Bool -> g -> m Bool
Gen
State of the random number generator
createSystemRandom :: IO GenIO 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 :: PrimMonad m => Word64 -> m (Gen (PrimState m)) Source #
Initialize a generator a single word.
>>>
initialize 0 >>= save
FrozenGen 1876011003808476466
withSystemRandom :: (GenIO -> IO a) -> IO a Source #
Seed with system random number. (/dev/urandom
on Unix-like
systems and CryptAPI on Windows).
withFrozen :: FrozenGen -> (forall s. Gen s -> ST s a) -> (a, FrozenGen) Source #
Run an action with a frozen generator, returning the result and the new frozen generator.
Getting random numbers
class Variate a where Source #
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.
uniformB :: Generator g m => a -> g -> m a Source #
Generate a uniformly distributed random vairate in the range
[0,b). For integral types the bound must be less than the max bound
of Word32
(4294967295). Behaviour is undefined for negative
bounds.
Instances
Variate Bool Source # | |
Variate Double Source # | |
Variate Float Source # | |
Variate Int Source # | |
Variate Int8 Source # | |
Variate Int16 Source # | |
Variate Int32 Source # | |
Variate Int64 Source # | |
Variate Word Source # | |
Variate Word8 Source # | |
Variate Word16 Source # | |
Variate Word32 Source # | |
Variate Word64 Source # | |
(Variate a, Variate b) => Variate (a, b) Source # | |
(Variate a, Variate b, Variate c) => Variate (a, b, c) Source # | |
(Variate a, Variate b, Variate c, Variate d) => Variate (a, b, c, d) Source # | |
advance :: PrimMonad m => Word64 -> Gen (PrimState m) -> m () Source #
Advance the given generator n steps in log(n) time. (Note that a
"step" is a single random 32-bit (or less) Variate
. Data types
such as Double
or Word64
require two "steps".)
>>>
create >>= \g -> replicateM_ 1000 (uniformW32 g) >> uniformW32 g
787992099>>>
create >>= \g -> replicateM_ 500 (uniformD g) >> uniformW32 g
787992099>>>
create >>= \g -> advance 1000 g >> uniformW32 g
787992099
retract :: PrimMonad m => Word64 -> Gen (PrimState m) -> m () Source #
Retract the given generator n steps in log(2^64-n) time. This
is just advance (-n)
.
>>>
create >>= \g -> replicateM 3 (uniformW32 g)
[1499251125,2252584727,2021901712]>>>
create >>= \g -> retract 1 g >> replicateM 3 (uniformW32 g)
[4031155981,1499251125,2252584727]
Seeds
Instances
Eq FrozenGen Source # | |
Data FrozenGen Source # | |
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> FrozenGen -> c FrozenGen # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c FrozenGen # toConstr :: FrozenGen -> Constr # dataTypeOf :: FrozenGen -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c FrozenGen) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FrozenGen) # gmapT :: (forall b. Data b => b -> b) -> FrozenGen -> FrozenGen # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> FrozenGen -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> FrozenGen -> r # gmapQ :: (forall d. Data d => d -> u) -> FrozenGen -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> FrozenGen -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen # | |
Ord FrozenGen Source # | |
Show FrozenGen Source # | |
Generic FrozenGen Source # | |
Storable FrozenGen Source # | |
RandomGen FrozenGen Source # | |
type Rep FrozenGen Source # | |
initFrozen :: Word64 -> FrozenGen Source #
Generate a new seed using single Word64
.
>>>
initFrozen 0
FrozenGen 1876011003808476466
Type restricted versions
uniform
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 #