Random v0.2.4 Random

This module contains pseudo-random number generators for various distributionsported from Python 3 random module The documentation below is adapted from that module as well.

For integers, there is uniform selection from a range. For sequences, there is uniform selection of a random element, a function to generate a random permutation, and a function for random sampling without replacement.

On the real line, there are functions to compute uniform, normal (Gaussian), lognormal, negative exponential, gamma, and beta distributions. For generating distributions of angles, the von Mises distribution is available.

Project homepage

Original Python 3 documentation

Example:

iex(1)> Random.seed(42)
:undefined
iex(2)> Random.randint(5, 142)
40
iex(3)> Random.randrange(5, 142, 2)
127
iex(4)> Random.choice(10..1000)
779

Link to this section Summary

Functions

Beta distribution

Returns a random element from a non-empty sequence

Exponential distribution. lambda is 1.0 divided by the desired mean. It should be nonzero. Returned values range from 0 to positive infinity if lambda is positive, and from negative infinity to 0 if lambda is negative

Gamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0

Gaussian distribution

Log normal distribution. If you take the natural logarithm of this distribution, you’ll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero

Return x % y

Normal distribution. mu is the mean, and sigma is the standard deviation

Pareto distribution

Return a random integer N such that a <= N <= b. Alias for Random.randrange(a, b+1)

Return the next random floating point number in the range [0.0, 1.0)

Returns a random integer from range [0, stop)

Returns a random integer from range [start, stop)

Returns a random integer from range [start, stop) with steps step

Chooses k unique random elements from a population sequence or set

Seed the random generator

Shuffle sequence x. This function is currently an alias for Enum.shuffle/1

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a

mu is the mean angle, expressed in radians between 0 and 2pi, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2pi

Weibull distribution

Link to this section Functions

Link to this function betavariate(alpha, beta)

Beta distribution.

Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1.

Returns a random element from a non-empty sequence.

If seq is a list, converts it to a tuple before picking.

Link to this function expovariate(lambda)

Exponential distribution. lambda is 1.0 divided by the desired mean. It should be nonzero. Returned values range from 0 to positive infinity if lambda is positive, and from negative infinity to 0 if lambda is negative.

Link to this function gammavariate(alpha, beta)

Gamma distribution. Not the gamma function! Conditions on the parameters are alpha > 0 and beta > 0.

The probability distribution function is:

          x ** (alpha - 1) * exp(-x / beta)
pdf(x) =  ---------------------------------
          gamma(alpha) * beta ** alpha
Link to this function gauss(mu, sigma, gauss_next \\ nil)

Gaussian distribution.

mu is the mean, and sigma is the standard deviation. This is slightly faster than the Random.normalvariate/2 function.

Returns {number, gauss_next}

Example:

iex(1)> {n, gauss_next} = Random.gauss(1, 2)
{-2.0056082102271917, 0.5561885306380824}
iex(2)> {n, gauss_next} = Random.gauss(1, 2, gauss_next)
{2.112377061276165, nil}
Link to this function lognormvariate(mu, sigma)

Log normal distribution. If you take the natural logarithm of this distribution, you’ll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.

Return x % y

Link to this function normalvariate(mu, sigma)

Normal distribution. mu is the mean, and sigma is the standard deviation.

Link to this function paretovariate(alpha)

Pareto distribution.

alpha is the shape parameter.

Return a random integer N such that a <= N <= b. Alias for Random.randrange(a, b+1).

Return the next random floating point number in the range [0.0, 1.0).

Link to this function randrange(stop)

Returns a random integer from range [0, stop).

Link to this function randrange(start, stop)

Returns a random integer from range [start, stop).

Link to this function randrange(start, stop, step)

Returns a random integer from range [start, stop) with steps step.

Chooses k unique random elements from a population sequence or set.

Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices).

Members of the population need not be unique. If the population contains repeats, then each occurrence is a possible selection in the sample.

To choose a sample in a range of integers, use range as an argument. This is especially fast and space efficient for sampling from a large population: Random.sample(0..10000000, 60)

Seed the random generator.

This function accepts both erlang (tuple of 3 integers) and python (single integer) forms of seeding.

Random.seed(n) is equivalent to Random.seed({0, n, 0}).

Erlang form:

now = :erlang.timestamp
Random.seed(now)

Python form:

Random.seed(5)
Link to this function shuffle(enumerable)

Shuffle sequence x. This function is currently an alias for Enum.shuffle/1.

Note that for even rather small size(x), the total number of permutations of x is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated.

Link to this function triangular(low \\ 0, high \\ 1, mode \\ nil)

Triangular distribution.

Return a random floating point number N such that low <= N <= high and with the specified mode between those bounds. The low and high bounds default to zero and one. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.

http://en.wikipedia.org/wiki/Triangular_distribution

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.

The end-point value b may or may not be included in the range depending on floating-point rounding in the equation a + (b-a) * random().

Link to this function vonmisesvariate(mu, kappa)

mu is the mean angle, expressed in radians between 0 and 2pi, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2pi.

Link to this function weibullvariate(alpha, beta)

Weibull distribution.

alpha is the scale parameter and beta is the shape parameter.