LambdaHack-0.11.0.1: A game engine library for tactical squad ASCII roguelike dungeon crawlers

Safe Haskell | None
Language | Haskell2010

Game.LambdaHack.Core.Random

Contents

- The `Rng` monad
- Random operations
- Fractional chance
- Casting dice scaled with level
- Specialized monadic folds
- Internal operations

Description

Representation of probabilities and random computations.

Synopsis

- type Rnd a = State SMGen a
- randomR :: Integral a => (a, a) -> Rnd a
- randomR0 :: Integral a => a -> Rnd a
- nextRandom :: Integral a => a -> SMGen -> (a, SMGen)
- randomWord32 :: Rnd Word32
- oneOf :: [a] -> Rnd a
- shuffle :: Eq a => [a] -> Rnd [a]
- invalidInformationCode :: Word16
- shuffleExcept :: Vector Word16 -> Int -> [Word16] -> Rnd [Word16]
- frequency :: Show a => Frequency a -> Rnd a
- type Chance = Rational
- chance :: Chance -> Rnd Bool
- castDice :: AbsDepth -> AbsDepth -> Dice -> Rnd Int
- oddsDice :: AbsDepth -> AbsDepth -> Dice -> Rnd Bool
- castDiceXY :: AbsDepth -> AbsDepth -> DiceXY -> Rnd (Int, Int)
- foldrM :: Foldable t => (a -> b -> Rnd b) -> b -> t a -> Rnd b
- foldlM' :: Foldable t => (b -> a -> Rnd b) -> b -> t a -> Rnd b
- rollFreq :: Show a => Frequency a -> SMGen -> (a, SMGen)

# The `Rng` monad

type Rnd a = State SMGen a Source #

The monad of computations with random generator state.

# Random operations

randomR :: Integral a => (a, a) -> Rnd a Source #

Get a random object within a (inclusive) range with a uniform distribution.

randomR0 :: Integral a => a -> Rnd a Source #

Generate random `Integral` in `[0, x]` range.

nextRandom :: Integral a => a -> SMGen -> (a, SMGen) Source #

Generate a random integral value in `[0, x]` range, where `x` is within `Int32`.

The limitation to `Int32` values is needed to keep it working on signed types. In package `random`, a much more complex scheme is used to keep it working for arbitrary fixed number of bits.

randomWord32 :: Rnd Word32 Source #

Get a random `Word32` using full range.

oneOf :: [a] -> Rnd a Source #

Get any element of a list with equal probability.

shuffle :: Eq a => [a] -> Rnd [a] Source #

Generates a random permutation. Naive, but good enough for small inputs.

invalidInformationCode :: Word16 Source #

Code that means the information (e.g., flavour or hidden kind index) should be regenerated, because it could not be transferred from previous playthrough (it's random in each playthrough or there was no previous playthrough).

shuffleExcept :: Vector Word16 -> Int -> [Word16] -> Rnd [Word16] Source #

Generates a random permutation, except for the existing mapping.

frequency :: Show a => Frequency a -> Rnd a Source #

Gen an element according to a frequency distribution.

# Fractional chance

type Chance = Rational Source #

Fractional chance.

chance :: Chance -> Rnd Bool Source #

Give `True`, with probability determined by the fraction.

# Casting dice scaled with level

castDice :: AbsDepth -> AbsDepth -> Dice -> Rnd Int Source #

Cast dice scaled with current level depth.

oddsDice :: AbsDepth -> AbsDepth -> Dice -> Rnd Bool Source #

Cast dice scaled with current level depth and return `True` if the results is greater than 50.

castDiceXY :: AbsDepth -> AbsDepth -> DiceXY -> Rnd (Int, Int) Source #

Cast dice, scaled with current level depth, for coordinates.

# Specialized monadic folds

foldrM :: Foldable t => (a -> b -> Rnd b) -> b -> t a -> Rnd b Source #

foldlM' :: Foldable t => (b -> a -> Rnd b) -> b -> t a -> Rnd b Source #

# Internal operations

rollFreq :: Show a => Frequency a -> SMGen -> (a, SMGen) Source #

Randomly choose an item according to the distribution.