random-extras-0.1: Additional functions for random values.

Portability portable experimental

Contents

Description

Additional monadic random functions, based on `MonadRandom`.

Synopsis

# Random functions

## Shuffling

shuffle :: MonadRandom m => [a] -> m [a]Source

Shuffle a list randomly. The method is based on Oleg Kiselyov's perfect shuffle http://okmij.org/ftp/Haskell/perfect-shuffle.txt, but much simpler because it uses existing data structures. The efficiency of both methods should be comparable.

Complexity: O(n * log n)

shuffleSeq :: (MonadRandom m, MonadPlus f) => Seq a -> m (f a)Source

Shuffle a sequence randomly. This is being used by `shuffle`, so it logically uses the same method.

Complexity: O(n * log n)

## Choice

choiceExtract :: MonadRandom m => [a] -> m (Maybe (a, [a]))Source

Randomly choose and extract an element from a list.

Complexity: O(n)

choiceExtractSeq :: MonadRandom m => Seq a -> m (Maybe (a, Seq a))Source

Randomly choose and extract an element from a sequence.

Complexity: O(log n)

choice :: MonadRandom m => [a] -> m aSource

Select a random element from a list.

Complexity: O(n).

choiceSeq :: MonadRandom m => Seq a -> m aSource

Select a random element from a sequence.

Complexity: O(log n).