Safe Haskell | None |
---|---|

Language | Haskell2010 |

This module provides functions to perform shuffles on mutable vectors. The shuffling is uniform amongst all permuations and uses the minimal number of transpositions.

## Synopsis

- shuffle :: forall m a g v. (PrimMonad m, RandomGen g, MVector v a) => v (PrimState m) a -> g -> m g
- shuffleM :: forall m a v. (PrimMonad m, MonadRandom m, MVector v a) => v (PrimState m) a -> m ()
- shuffleK :: forall m a v. (PrimMonad m, MonadRandom m, MVector v a) => Int -> v (PrimState m) a -> m ()
- maximalCycle :: forall m a g v. (PrimMonad m, RandomGen g, MVector v a) => v (PrimState m) a -> g -> m g
- maximalCycleM :: forall m a v. (PrimMonad m, MonadRandom m, MVector v a) => v (PrimState m) a -> m ()
- derangement :: forall m a g v. (PrimMonad m, RandomGen g, Eq a, MVector v a) => v (PrimState m) a -> g -> m g
- derangementM :: (PrimMonad m, MonadRandom m, Eq a, MVector v a) => v (PrimState m) a -> m ()

# Documentation

shuffle :: forall m a g v. (PrimMonad m, RandomGen g, MVector v a) => v (PrimState m) a -> g -> m g Source #

Perform a shuffle on a mutable vector with a given random generator, returning a new random generator.

This uses the Fisher--Yates--Knuth algorithm

shuffleM :: forall m a v. (PrimMonad m, MonadRandom m, MVector v a) => v (PrimState m) a -> m () Source #

Perform a shuffle on a mutable vector in a monad which has a source of randomness.

This uses the Fisher--Yates--Knuth algorithm

shuffleK :: forall m a v. (PrimMonad m, MonadRandom m, MVector v a) => Int -> v (PrimState m) a -> m () Source #

Shuffle the first k elements of a vector.

maximalCycle :: forall m a g v. (PrimMonad m, RandomGen g, MVector v a) => v (PrimState m) a -> g -> m g Source #

Perform a shuffle on a mutable vector wherein the shuffled indices form a maximal cycle.

This uses the Sattolo algorithm.

maximalCycleM :: forall m a v. (PrimMonad m, MonadRandom m, MVector v a) => v (PrimState m) a -> m () Source #

Perform a shuffle on a mutable vector wherein the shuffled indices form a maximal cycle in a monad with a source of randomness.

This uses the Sattolo algorithm.

derangement :: forall m a g v. (PrimMonad m, RandomGen g, Eq a, MVector v a) => v (PrimState m) a -> g -> m g Source #

Perform a derangement on a mutable vector with a given random generator, returning a new random generator.

**Note:** It is assumed the input vector consists of distinct values.

This uses the "early refusal" algorithm.

derangementM :: (PrimMonad m, MonadRandom m, Eq a, MVector v a) => v (PrimState m) a -> m () Source #

Perform a derangement on a mutable vector in a monad which has a source of randomness.

**Note:** It is assumed the input vector consists of distinct values.

This uses the "early refusal" algorithm