foundation-0.0.30: Alternative prelude with batteries and no dependencies

License | BSD-style
Stability | experimental
Portability | Good
Safe Haskell | Safe-Inferred
Language | Haskell2010

Foundation.Random

Description

This module deals with the random subsystem abstractions.

It provide 2 different set of abstractions:

- The first abstraction that allow a monad to generate random through the `MonadRandom` class.
- The second abstraction to make generic random generator `RandomGen` and a small State monad like wrapper `MonadRandomState` to abstract a generator.

Synopsis

- class (Functor m, Applicative m, Monad m) => MonadRandom m where
  - getRandomBytes :: CountOf Word8 -> m (UArray Word8)
  - getRandomWord64 :: m Word64
  - getRandomF32 :: m Float
  - getRandomF64 :: m Double
- class RandomGen gen where
  - randomNew :: MonadRandom m => m gen
  - randomNewFrom :: UArray Word8 -> Maybe gen
  - randomGenerate :: CountOf Word8 -> gen -> (UArray Word8, gen)
  - randomGenerateWord64 :: gen -> (Word64, gen)
  - randomGenerateF32 :: gen -> (Float, gen)
  - randomGenerateF64 :: gen -> (Double, gen)
- newtype MonadRandomState gen a = MonadRandomState {
  - runRandomState :: gen -> (a, gen)
  }
- withRandomGenerator :: RandomGen gen => gen -> MonadRandomState gen a -> (a, gen)
- type RNG = RNGv1
- type RNGv1 = State

# Documentation

class (Functor m, Applicative m, Monad m) => MonadRandom m where Source #

A monad constraint that allows to generate random bytes

Methods

getRandomBytes :: CountOf Word8 -> m (UArray Word8) Source #

getRandomWord64 :: m Word64 Source #

getRandomF32 :: m Float Source #

getRandomF64 :: m Double Source #

#### Instances

Instances details

MonadRandom IO Source # |
Instance details Defined in Foundation.Random.Class Methods getRandomBytes :: CountOf Word8 -> IO (UArray Word8) Source # getRandomWord64 :: IO Word64 Source # getRandomF32 :: IO Float Source # getRandomF64 :: IO Double Source #
RandomGen gen => MonadRandom (MonadRandomState gen) Source # |
Instance details Defined in Foundation.Random.DRG Methods getRandomBytes :: CountOf Word8 -> MonadRandomState gen (UArray Word8) Source # getRandomWord64 :: MonadRandomState gen Word64 Source # getRandomF32 :: MonadRandomState gen Float Source # getRandomF64 :: MonadRandomState gen Double Source #

class RandomGen gen where Source #

A Deterministic Random Generator (DRG) class

Methods

randomNew :: MonadRandom m => m gen Source #

Initialize a new random generator

randomNewFrom :: UArray Word8 -> Maybe gen Source #

Initialize a new random generator from a binary seed.

If `Nothing` is returned, then the data is not acceptable for creating a new random generator.

randomGenerate :: CountOf Word8 -> gen -> (UArray Word8, gen) Source #

Generate N bytes of randomness from a DRG

randomGenerateWord64 :: gen -> (Word64, gen) Source #

Generate a Word64 from a DRG

randomGenerateF32 :: gen -> (Float, gen) Source #

randomGenerateF64 :: gen -> (Double, gen) Source #

newtype MonadRandomState gen a Source #

A simple Monad class very similar to a State Monad with the state being a RandomGenerator.

Constructors

MonadRandomState |

Fields

- runRandomState :: gen -> (a, gen)

#### Instances

Instances details

Applicative (MonadRandomState gen) Source # |

Instance details

Defined in Foundation.Random.DRG

Methods

pure :: a -> MonadRandomState gen a Source #

(<*>) :: MonadRandomState gen (a -> b) -> MonadRandomState gen a -> MonadRandomState gen b Source #

liftA2 :: (a -> b -> c) -> MonadRandomState gen a -> MonadRandomState gen b -> MonadRandomState gen c Source #

(*>) :: MonadRandomState gen a -> MonadRandomState gen b -> MonadRandomState gen b Source #

(<*) :: MonadRandomState gen a -> MonadRandomState gen b -> MonadRandomState gen a Source #

Functor (MonadRandomState gen) Source # |
Instance details Defined in Foundation.Random.DRG Methods fmap :: (a -> b) -> MonadRandomState gen a -> MonadRandomState gen b Source # (<$) :: a -> MonadRandomState gen b -> MonadRandomState gen a Source #
Monad (MonadRandomState gen) Source # |
Instance details Defined in Foundation.Random.DRG Methods (>>=) :: MonadRandomState gen a -> (a -> MonadRandomState gen b) -> MonadRandomState gen b Source # (>>) :: MonadRandomState gen a -> MonadRandomState gen b -> MonadRandomState gen b Source # return :: a -> MonadRandomState gen a Source #
RandomGen gen => MonadRandom (MonadRandomState gen) Source # |
Instance details Defined in Foundation.Random.DRG Methods getRandomBytes :: CountOf Word8 -> MonadRandomState gen (UArray Word8) Source # getRandomWord64 :: MonadRandomState gen Word64 Source # getRandomF32 :: MonadRandomState gen Float Source # getRandomF64 :: MonadRandomState gen Double Source #

withRandomGenerator :: RandomGen gen => gen -> MonadRandomState gen a -> (a, gen) Source #

Run a pure computation with a Random Generator in the `MonadRandomState`

type RNG = RNGv1 Source #

An alias to the default choice of deterministic random number generator

Unless, you want to have the stability of a specific random number generator, e.g. for tests purpose, it's recommended to use this alias so that you would keep up to date with possible bugfixes, or change of algorithms.

type RNGv1 = State Source #