foundation-0.0.3: Alternative prelude with batteries and no dependencies

License | BSD-style
Stability | experimental
Portability | Good
Safe Haskell | None
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
- newtype MonadRandomState gen a = MonadRandomState {
  - runRandomState :: gen -> (a, gen)
  }
- class RandomGen gen where
- withRandomGenerator :: RandomGen gen => gen -> MonadRandomState gen a -> (a, gen)
- type RNG = RNGv1
- data RNGv1

# Documentation

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

A monad constraint that allows to generate random bytes

Minimal complete definition

getRandomBytes

Methods

getRandomBytes :: Int -> m (UArray Word8) #

Instances

MonadRandom IO # |
Methods getRandomBytes :: Int -> IO (UArray Word8) #
RandomGen gen => MonadRandom (MonadRandomState gen) # |
Methods getRandomBytes :: Int -> MonadRandomState gen (UArray Word8) #

newtype MonadRandomState gen a #

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

Constructors

MonadRandomState |
Fields runRandomState :: gen -> (a, gen)

Instances

Monad (MonadRandomState gen) # |
Methods (>>=) :: MonadRandomState gen a -> (a -> MonadRandomState gen b) -> MonadRandomState gen b # (>>) :: MonadRandomState gen a -> MonadRandomState gen b -> MonadRandomState gen b # return :: a -> MonadRandomState gen a # fail :: String -> MonadRandomState gen a #
Functor (MonadRandomState gen) # |
Methods fmap :: (a -> b) -> MonadRandomState gen a -> MonadRandomState gen b # (<$) :: a -> MonadRandomState gen b -> MonadRandomState gen a #
Applicative (MonadRandomState gen) # |
Methods pure :: a -> MonadRandomState gen a # (<*>) :: MonadRandomState gen (a -> b) -> MonadRandomState gen a -> MonadRandomState gen b # (*>) :: MonadRandomState gen a -> MonadRandomState gen b -> MonadRandomState gen b # (<*) :: MonadRandomState gen a -> MonadRandomState gen b -> MonadRandomState gen a #
RandomGen gen => MonadRandom (MonadRandomState gen) # |
Methods getRandomBytes :: Int -> MonadRandomState gen (UArray Word8) #

class RandomGen gen where #

A Deterministic Random Generator (DRG) class

Minimal complete definition

randomNew, randomGenerate

Methods

randomNew :: MonadRandom m => m gen #

Initialize a new random generator

randomGenerate :: Int -> gen -> (UArray Word8, gen) #

Generate N bytes of randomness from a DRG

Instances

RandomGen RNGv1 # |
Methods randomNew :: MonadRandom m => m RNGv1 # randomGenerate :: Int -> RNGv1 -> (UArray Word8, RNGv1) #

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

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

type RNG = RNGv1 #

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.

data RNGv1 #

RNG based on ChaCha core.

The algorithm is identical to the arc4random found in recent BSDs, namely a ChaCha core provide 64 bytes of random from 32 bytes of key.

Instances

RandomGen RNGv1 # |
Methods randomNew :: MonadRandom m => m RNGv1 # randomGenerate :: Int -> RNGv1 -> (UArray Word8, RNGv1) #