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

Portabilityportable
Stabilityexperimental
Safe HaskellSafe-Infered

Data.Random.Extras

Contents

Description

Additional monadic random functions, based on random-fu.

Synopsis

Random functions

Shuffling

shuffle :: [a] -> RVar [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), where n is the length of the input list.

shuffleSeq :: Seq a -> RVar [a]Source

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

Complexity: O(n * log n), where n is the length of the input sequence.

Sampling

sample :: Int -> [a] -> RVar [a]Source

Take a random sample from a list.

Complexity: O(n + m * log n), where n is the length of the input list and m is the sample size.

sampleSeq :: Int -> Seq a -> RVar [a]Source

Take a random sample from a sequence.

Complexity: O(m * log n), where n is the length of the input sequence and m is the sample size.

Choice

choiceExtract :: [a] -> Maybe (RVar ([a], a))Source

Randomly choose and extract an element from a list.

Complexity: O(n), where n is the length of the input list.

choiceExtractSeq :: Seq a -> Maybe (RVar (Seq a, a))Source

Randomly choose and extract an element from a sequence.

Complexity: O(log n), where n is the length of the input sequence.

choice :: [a] -> RVar aSource

Select a random element from a list.

Partial function: This function is only defined on non-empty lists.

Complexity: O(n), where n is the length of the input list.

safeChoice :: [a] -> Maybe (RVar a)Source

Safely select a random element from a list.

Complexity: O(n), where n is the length of the input list.

iterativeChoice :: [a] -> RVar aSource

Select a random element from a list, traversing the list only once.

Partial function: This function is only defined on non-empty lists with a length below (maxBound + 1 :: Int).

Complexity: O(n), where n is the length of the input list.

choiceSeq :: Seq a -> RVar aSource

Select a random element from a sequence.

Partial function: This function is only defined on non-empty sequences.

Complexity: O(log n), where n is the length of the input sequence.

safeChoiceSeq :: Seq a -> Maybe (RVar a)Source

Safely select a random element from a sequence.

Complexity: O(log n), where n is the length of the input sequence.

choiceArray :: (IArray arr a, Ix i, Distribution Uniform i) => arr i a -> RVar aSource

Select a random element from an array.

Complexity: O(1).

Choices

choices :: Int -> [a] -> RVar [a]Source

A stream of random elements from a list.

Partial function: This function is only defined on non-empty lists.

Complexity: O(n) base and O(1) per element.

safeChoices :: Int -> [a] -> Maybe (RVar [a])Source

Safely get a stream of random elements from a list.

Complexity: O(n) base and O(1) per element, where n is the length of the input list.

choicesArray :: (IArray arr a, Ix i, Distribution Uniform i) => Int -> arr i a -> RVar [a]Source

A stream of random elements from an array.

Complexity: O(1) per element.