packages feed

crypto-rng-effectful-1.0.0.0: src/Effectful/Crypto/RNG/Unsafe.hs

-- | Generation of random numbers via "Crypto.RNG.Unsafe".
module Effectful.Crypto.RNG.Unsafe
  ( -- * Effect
    RNG(..)
  , CryptoRNG(..)

    -- ** Handlers
  , runRNG

    -- * Instantiation of the initial RNG state
  , RNGState
  , newRNGState
  ) where

import Crypto.RNG.Unsafe
import Effectful
import Effectful.Dispatch.Dynamic
import qualified System.Random as R

import Effectful.Crypto.RNG.Effect

-- | Generate random numbers, starting with a given initial 'RNGState'.
--
-- /Note:/ random numbers generated by this interpreter are not
-- cryptographically secure and should only be used for testing purposes.
runRNG :: IOE :> es => RNGState -> Eff (RNG : es) a -> Eff es a
runRNG rng = interpret $ \_ -> \case
  RandomBytes n  -> withRNG rng $ \g -> R.genByteString n g
  Random         -> withRNG rng $ \g -> R.uniform g
  RandomR bounds -> withRNG rng $ \g -> R.uniformR bounds g