module Foundation.Random
( MonadRandom(..)
, MonadRandomState(..)
, RandomGen(..)
, getRandomPrimType
, withRandomGenerator
, RNG
, RNGv1
) where
import Foundation.Internal.Base
import Foundation.Primitive.Types.OffsetSize
import Foundation.Internal.Proxy
import Foundation.Primitive.Monad
import Foundation.System.Entropy
import Foundation.Array
import qualified Foundation.Array.Unboxed as A
import qualified Foundation.Array.Unboxed.Mutable as A
import GHC.ST
import qualified Prelude
class (Functor m, Applicative m, Monad m) => MonadRandom m where
getRandomBytes :: CountOf Word8 -> m (UArray Word8)
instance MonadRandom IO where
getRandomBytes = getEntropy
class RandomGen gen where
randomNew :: MonadRandom m => m gen
randomNewFrom :: UArray Word8 -> Maybe gen
randomGenerate :: CountOf Word8 -> gen -> (UArray Word8, gen)
newtype MonadRandomState gen a = MonadRandomState { runRandomState :: gen -> (a, gen) }
instance Functor (MonadRandomState gen) where
fmap f m = MonadRandomState $ \g1 ->
let (a, g2) = runRandomState m g1 in (f a, g2)
instance Applicative (MonadRandomState gen) where
pure a = MonadRandomState $ \g -> (a, g)
(<*>) fm m = MonadRandomState $ \g1 ->
let (f, g2) = runRandomState fm g1
(a, g3) = runRandomState m g2
in (f a, g3)
instance Monad (MonadRandomState gen) where
return a = MonadRandomState $ \g -> (a, g)
(>>=) m1 m2 = MonadRandomState $ \g1 ->
let (a, g2) = runRandomState m1 g1
in runRandomState (m2 a) g2
instance RandomGen gen => MonadRandom (MonadRandomState gen) where
getRandomBytes n = MonadRandomState (randomGenerate n)
getRandomPrimType :: forall randomly ty . (PrimType ty, MonadRandom randomly) => randomly ty
getRandomPrimType =
flip A.index 0 . A.unsafeRecast <$> getRandomBytes (A.primSizeInBytes (Proxy :: Proxy ty))
withRandomGenerator :: RandomGen gen
=> gen
-> MonadRandomState gen a
-> (a, gen)
withRandomGenerator gen m = runRandomState m gen
type RNG = RNGv1
newtype RNGv1 = RNGv1 (UArray Word8)
instance RandomGen RNGv1 where
randomNew = RNGv1 <$> getRandomBytes 32
randomNewFrom bs
| A.length bs == 32 = Just $ RNGv1 bs
| otherwise = Nothing
randomGenerate = rngv1Generate
rngv1KeySize :: CountOf Word8
rngv1KeySize = 32
rngv1Generate :: CountOf Word8 -> RNGv1 -> (UArray Word8, RNGv1)
rngv1Generate n@(CountOf x) (RNGv1 key) = runST $ do
dst <- A.newPinned n
newKey <- A.newPinned rngv1KeySize
A.withMutablePtr dst $ \dstP ->
A.withMutablePtr newKey $ \newKeyP ->
A.withPtr key $ \keyP -> do
_ <- unsafePrimFromIO $ c_rngv1_generate newKeyP dstP keyP (Prelude.fromIntegral x)
return ()
(,) <$> A.unsafeFreeze dst
<*> (RNGv1 <$> A.unsafeFreeze newKey)
foreign import ccall unsafe "foundation_rngV1_generate"
c_rngv1_generate :: Ptr Word8
-> Ptr Word8
-> Ptr Word8
-> Word32
-> IO Word32