Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
MCSP.System.Random
Synopsis
- data Random a
- evalRandom :: Generator g (ST s) => Random a -> g -> ST s a
- liftRandom :: (forall g m. Generator g m => g -> m a) -> Random a
- lazyRandom :: Random a -> Random a
- generate :: Random a -> IO a
- generateFast :: Random a -> IO a
- type Seed = Pair Word64
- readSeedP :: ReadP Seed
- readSeed :: String -> Seed
- showSeedS :: Seed -> ShowS
- showSeed :: Seed -> String
- generateFastWith :: Seed -> Random a -> a
- generateWith :: Seed -> Random a -> a
- randomSeed :: IO Seed
- class Variate a
- uniform :: Variate a => Random a
- uniformR :: Variate a => a -> a -> Random a
- uniformB :: Variate a => a -> Random a
- uniformE :: (Enum a, Bounded a) => Random a
- uniformRE :: Enum a => a -> a -> Random a
- choice :: NonEmpty a -> Random a
- weightedChoice :: NonEmpty (Double, a) -> Random a
- shuffle :: IsList l => l -> Random l
- partitions :: Vector v a => v a -> Random [v a]
- repeatR :: Random a -> Random [a]
- iterateR :: (a -> Random a) -> a -> Random (NonEmpty a)
Random Monad
A monad capable of producing random values of 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 #
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
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)
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
Minimal complete definition
uniform, uniformR, uniformB
Instances
Variate Int16 | |
Variate Int32 | |
Variate Int64 | |
Variate Int8 | |
Variate Word16 | |
Variate Word32 | |
Variate Word64 | |
Variate Word8 | |
Variate Bool | |
Variate Double | |
Variate Float | |
Variate Int | |
Variate Word | |
(Variate a, Variate b) => Variate (a, b) | |
Defined in System.Random.PCG.Class | |
(Variate a, Variate b, Variate c) => Variate (a, b, c) | |
Defined in System.Random.PCG.Class | |
(Variate a, Variate b, Variate c, Variate d) => Variate (a, b, c, d) | |
Defined in System.Random.PCG.Class |
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
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]]