mcsp-algorithms-0.1.0: Algorithms for Minimum Common String Partition (MCSP) in Haskell.
Safe HaskellSafe-Inferred
LanguageGHC2021

MCSP.System.Random

Description

Randomized operations using a Random monad for PCG operations.

Synopsis

Random Monad

data Random a Source #

A monad capable of producing random values of a.

Instances

Instances details
Applicative Random Source # 
Instance details

Defined in MCSP.System.Random.Monad

Methods

pure :: a -> Random a #

(<*>) :: Random (a -> b) -> Random a -> Random b #

liftA2 :: (a -> b -> c) -> Random a -> Random b -> Random c #

(*>) :: Random a -> Random b -> Random b #

(<*) :: Random a -> Random b -> Random a #

Functor Random Source # 
Instance details

Defined in MCSP.System.Random.Monad

Methods

fmap :: (a -> b) -> Random a -> Random b #

(<$) :: a -> Random b -> Random a #

Monad Random Source # 
Instance details

Defined in MCSP.System.Random.Monad

Methods

(>>=) :: Random a -> (a -> Random b) -> Random b #

(>>) :: Random a -> Random b -> Random b #

return :: a -> Random a #

Monoid a => Monoid (Random a) Source # 
Instance details

Defined in MCSP.System.Random.Monad

Methods

mempty :: Random a #

mappend :: Random a -> Random a -> Random a #

mconcat :: [Random a] -> Random a #

Semigroup a => Semigroup (Random a) Source # 
Instance details

Defined in MCSP.System.Random.Monad

Methods

(<>) :: Random a -> Random a -> Random a #

sconcat :: NonEmpty (Random a) -> Random a #

stimes :: Integral b => b -> Random a -> Random a #

evalRandom :: Generator g (ST s) => Random a -> g -> ST s a Source #

Evaluate a random computation with the given initial generator and return the final value.

liftRandom :: (forall g m. Generator g m => g -> m a) -> Random a Source #

Turn a standard RNG function into a Random monad.

lazyRandom :: Random a -> Random a Source #

Allows a Random monad to be evaluated lazily.

This function should be applied with care, otherwise a strict Random monad could still force early evaluation of this lazy version.

generate :: Random a -> IO a Source #

Generate value using a random seed with the Standard PCG Generator.

>>> import MCSP.System.Random.Monad (liftRandom)
>>> import Data.Function (id)
>>> import System.Random.PCG.Class (uniform1)
>>> generate (liftRandom (uniform1 id))
...

generateFast :: Random a -> IO a Source #

Generate value using a random seed with the Fast PCG Generator.

>>> import MCSP.System.Random.Monad (liftRandom)
>>> import Data.Function (id)
>>> import System.Random.PCG.Class (uniform1)
>>> generateFast (liftRandom (uniform1 id))
...

Evaluation

type Seed = Pair Word64 Source #

Values used to seed a random number generator.

readSeedP :: ReadP Seed Source #

Parser combinator for reading seeds.

readSeed :: String -> Seed Source #

Read a seed in hexadecimal format.

Inverse of showSeed.

>>> readSeed "0 1"
(0,1)
>>> readSeed (showSeed (5, 10))
(5,10)
>>> readSeed " 75f9fea579c63117 8a3a15e4c0a7029f "
(8501105758304612631,9960297598112170655)

showSeedS :: Seed -> ShowS Source #

ShowS implementation for hexadecimal seed.

showSeed :: Seed -> String Source #

String representing the RNG seed in hexadecimal.

Inverse of readSeed.

>>> showSeed (0, 1)
"0 1"

generateFastWith :: Seed -> Random a -> a Source #

Use given seed to generate value using the Fast PCG generator.

>>> import MCSP.System.Random.Monad (liftRandom)
>>> import Data.Function (id)
>>> import System.Random.PCG.Class (uniform1)
>>> generateFastWith (100,200) (liftRandom (uniform1 id))
77824

generateWith :: Seed -> Random a -> a Source #

Use given seed to generate value using the Standard PCG generator.

>>> import MCSP.System.Random.Monad (liftRandom)
>>> import Data.Function (id)
>>> import System.Random.PCG.Class (uniform1)
>>> generateWith (100,200) (liftRandom (uniform1 id))
717541362

randomSeed :: IO Seed Source #

Generate a new random seed.

>>> randomSeed
...

Random Values

class Variate a #

Minimal complete definition

uniform, uniformR, uniformB

Instances

Instances details
Variate Int16 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Int16

uniformR :: Generator g m => (Int16, Int16) -> g -> m Int16

uniformB :: Generator g m => Int16 -> g -> m Int16

Variate Int32 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Int32

uniformR :: Generator g m => (Int32, Int32) -> g -> m Int32

uniformB :: Generator g m => Int32 -> g -> m Int32

Variate Int64 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Int64

uniformR :: Generator g m => (Int64, Int64) -> g -> m Int64

uniformB :: Generator g m => Int64 -> g -> m Int64

Variate Int8 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Int8

uniformR :: Generator g m => (Int8, Int8) -> g -> m Int8

uniformB :: Generator g m => Int8 -> g -> m Int8

Variate Word16 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Word16

uniformR :: Generator g m => (Word16, Word16) -> g -> m Word16

uniformB :: Generator g m => Word16 -> g -> m Word16

Variate Word32 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Word32

uniformR :: Generator g m => (Word32, Word32) -> g -> m Word32

uniformB :: Generator g m => Word32 -> g -> m Word32

Variate Word64 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Word64

uniformR :: Generator g m => (Word64, Word64) -> g -> m Word64

uniformB :: Generator g m => Word64 -> g -> m Word64

Variate Word8 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Word8

uniformR :: Generator g m => (Word8, Word8) -> g -> m Word8

uniformB :: Generator g m => Word8 -> g -> m Word8

Variate Bool 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Bool

uniformR :: Generator g m => (Bool, Bool) -> g -> m Bool

uniformB :: Generator g m => Bool -> g -> m Bool

Variate Double 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Double

uniformR :: Generator g m => (Double, Double) -> g -> m Double

uniformB :: Generator g m => Double -> g -> m Double

Variate Float 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Float

uniformR :: Generator g m => (Float, Float) -> g -> m Float

uniformB :: Generator g m => Float -> g -> m Float

Variate Int 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Int

uniformR :: Generator g m => (Int, Int) -> g -> m Int

uniformB :: Generator g m => Int -> g -> m Int

Variate Word 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m Word

uniformR :: Generator g m => (Word, Word) -> g -> m Word

uniformB :: Generator g m => Word -> g -> m Word

(Variate a, Variate b) => Variate (a, b) 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m (a, b)

uniformR :: Generator g m => ((a, b), (a, b)) -> g -> m (a, b)

uniformB :: Generator g m => (a, b) -> g -> m (a, b)

(Variate a, Variate b, Variate c) => Variate (a, b, c) 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m (a, b, c)

uniformR :: Generator g m => ((a, b, c), (a, b, c)) -> g -> m (a, b, c)

uniformB :: Generator g m => (a, b, c) -> g -> m (a, b, c)

(Variate a, Variate b, Variate c, Variate d) => Variate (a, b, c, d) 
Instance details

Defined in System.Random.PCG.Class

Methods

uniform :: Generator g m => g -> m (a, b, c, d)

uniformR :: Generator g m => ((a, b, c, d), (a, b, c, d)) -> g -> m (a, b, c, d)

uniformB :: Generator g m => (a, b, c, d) -> g -> m (a, b, c, d)

uniform :: Variate a => Random a Source #

O(1) Generate a uniformly distributed random variate.

  • Use entire range for integral types.
  • Use (0,1] range for floating types.
>>> import Prelude (Double)
>>> generateWith @Double (1,2) uniform
0.6502342391751404

uniformR :: Variate a => a -> a -> Random a Source #

O(1) Generate a uniformly distributed random variate in the given range.

  • Use inclusive range for integral types.
  • Use (a,b] range for floating types.
>>> generateWith (1,2) $ uniformR @Int 10 50
16

uniformB :: Variate a => a -> Random a Source #

O(1) Generate a uniformly distributed random variate 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.
>>> generateWith (1,2) $ uniformB @Int 200
143

uniformE :: (Enum a, Bounded a) => Random a Source #

O(1) Generate a uniformly distributed random variate.

It should be equivalent to uniformR (minBound, maxBound), but for non-Variate.

>>> import Prelude (Show)
>>> data T = A | B | C | D deriving (Enum, Bounded, Show)
>>> generateWith @T (1,3) uniformE
C

uniformRE :: Enum a => a -> a -> Random a Source #

O(1) Generate a uniformly distributed random variate in the given inclusive range.

It should be equivalent to uniformR, but for non-Variate.

>>> generateWith (1,3) $ uniformRE @Int 0 10
5
>>> generateWith (1,3) $ uniformRE 'a' 'z'
'n'

choice :: NonEmpty a -> Random a Source #

O(1) Choose a single random value from a non-empty list.

>>> generateWith (1,2) $ choice ["hi", "hello", "ola"]
"hello"

weightedChoice :: NonEmpty (Double, a) -> Random a Source #

O(n) Choose a single random value from a non-empty with probability proportional to weight.

>>> import Control.Monad (replicateM)
>>> generateWith (1,2) $ replicateM 10 $ weightedChoice [(1, "hi"), (10, "hello")]
["hello","hello","hello","hello","hi","hello","hello","hello","hello","hello"]

shuffle :: IsList l => l -> Random l Source #

O(?) Shuffles a list randomly.

>>> generateWith (1,2) $ shuffle @[Int] [1..5]
[4,1,2,3,5]

partitions :: Vector v a => v a -> Random [v a] Source #

O(n) Generate random partitions of a vector.

>>> generateWith (1,4) $ partitions @Vector @Int [1..10]
[[1,2,3],[4,5,6,7,8],[9],[10]]

repeatR :: Random a -> Random [a] Source #

Generates an infinite list of random values.

>>> import Prelude (take, (<$>))
>>> generateWith (1,3) (take 3 <$> repeatR (uniform @Int))
[-8858759364290496978,-2446124676014956441,1387719708863309077]

iterateR :: (a -> Random a) -> a -> Random (NonEmpty a) Source #

Random version of iterate.

>>> import Prelude ((<$>), (.), liftA2)
>>> import Data.List.NonEmpty (take)
>>> generateWith (1,3) $ take 3 <$> iterateR (liftA2 (+) uniform . pure) 2
[2.0,2.0197656927151786,2.3871610084947057]