Portability | portable |
---|---|

Stability | experimental |

Additional monadic random functions, based on `MonadRandom`

.

- shuffle :: MonadRandom m => [a] -> m [a]
- shuffleSeq :: MonadRandom m => Seq a -> m [a]
- sample :: MonadRandom m => Int -> [a] -> m [a]
- sampleSeq :: MonadRandom m => Int -> Seq a -> m [a]
- choiceExtract :: MonadRandom m => [a] -> m (Maybe ([a], a))
- choiceExtractSeq :: MonadRandom m => Seq a -> m (Maybe (Seq a, a))
- choice :: MonadRandom m => [a] -> m a
- choiceSeq :: MonadRandom m => Seq a -> m a
- choiceArray :: (MonadRandom m, IArray arr a, Ix i, Random i) => arr i a -> m a
- choices :: MonadRandom m => Int -> [a] -> m [a]
- choicesArray :: (MonadRandom m, IArray arr a, Ix i, Random i) => Int -> arr i a -> m [a]

# 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 => Seq a -> m [a]Source

Shuffle a sequence randomly. This is being used by `shuffle`

,
so it logically uses the same method.

Complexity: O(n * log n)

## Sampling

sample :: MonadRandom m => Int -> [a] -> m [a]Source

Take a random sample from a list.

Complexity: O(n + m * log n)

sampleSeq :: MonadRandom m => Int -> Seq a -> m [a]Source

Take a random sample from a sequence.

Complexity: O(m * 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 (Seq a, 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).

choiceArray :: (MonadRandom m, IArray arr a, Ix i, Random i) => arr i a -> m aSource

Select a random element from an array.

Complexity: O(1).

## Choices

choices :: MonadRandom m => Int -> [a] -> m [a]Source

Select *m* random elements from a list.

Complexity: O(m)

choicesArray :: (MonadRandom m, IArray arr a, Ix i, Random i) => Int -> arr i a -> m [a]Source

Select *m* random elements from an array.

Complexity: O(m)