pub fn[R : @contract.Rng] bernoulli(r : R, p~ : Double = 0.5) -> Bool
true with probability p, and false the rest of the time, so that a thousand calls hand back about 1000p of them. p outside [0, 1] is answered by the nearer end: certainty or impossibility.
pub fn[R : @contract.Rng] betavariate(r : R, alpha : Double, beta : Double) -> Double
A beta draw with shape parameters alpha and beta: two gamma draws, taken as a fraction of their sum.
pub fn[R : @contract.Rng] binomialvariate(r : R, n : Int64, p : Double) -> Int64
The number of successes in n trials with success probability p, by inverting the binomial CDF term by term: exact for any n, and linear in the number of successes drawn.
pub fn[R : @contract.Rng] cauchy(r : R, loc~ : Double = 0.0, scale~ : Double = 1.0) -> Double
A Cauchy draw: the location loc and the scale scale. Its mean and variance do not exist, so the median is the only centre it has — and a sample of it will surprise anyone who expects one.
pub fn[R : @contract.Rng] chi_squared(r : R, df : Double) -> Double
A chi-squared draw with k degrees of freedom: a gamma draw of shape k/2 and scale two, which is what the chi-squared distribution is.
pub fn[R : @contract.Rng, T] choice(r : R, items : ArrayView[T]) -> T?
One element, or None for an empty view.
pub fn[R : @contract.Rng, T] choices(r : R, items : ArrayView[T], k~ : Int32 = 1, weights? : ArrayView[Double]) -> Array[T]
k elements with replacement; with weights they are drawn proportionally.
pub fn[R : @contract.Rng] dirichlet(r : R, alpha : ArrayView[Double]) -> Array[Double]
A Dirichlet draw of concentration alpha: one gamma draw per component, each scaled down by their sum, so the result adds up to one. Every component of alpha must be positive.
pub fn[R : @contract.Rng] expovariate(r : R, lambd : Double) -> Double
An exponential draw with rate lambd (mean 1/lambd).
pub fn[R : @contract.Rng] fisher_f(r : R, d1 : Double, d2 : Double) -> Double
An F draw with d1 and d2 degrees of freedom: the ratio of two chi-squared draws, each divided by its own degrees of freedom.
pub fn[R : @contract.Rng] gammavariate(r : R, alpha : Double, beta : Double) -> Double
A gamma draw with shape alpha and scale beta, by Marsaglia and Tsang's transform-reject method with the squeeze from their paper (and the Cheng variant for alpha below one).
pub fn[R : @contract.Rng] gauss(r : R, mu~ : Double = 0.0, sigma~ : Double = 1.0) -> Double
The distributions, one draw each, over any Rng. Names follow Python's random where they overlap; several more are built from those by the definition of the distribution.
A draw takes as many words from the stream as its algorithm needs, so two distributions asked of the same generator interleave in the order you ask. Nothing is cached between calls: a normal draw is one draw, not one of a stored pair.
A normal draw, scaled to mu and sigma (Box-Muller).
pub fn[R : @contract.Rng] geometric(r : R, p : Double) -> Int64
The number of trials up to and including the first success, with success probability p: the support is 1, 2, 3, and the mean is 1/p.
pub fn[R : @contract.Rng] getrandbits(r : R, k : Int32) -> UInt64
k low bits of the next word; k over 64 returns the whole word.
pub fn[R : @contract.Rng] gumbel(r : R, loc~ : Double = 0.0, scale~ : Double = 1.0) -> Double
A Gumbel draw, the distribution of a maximum: location loc, scale scale, mean loc + 0.5772 scale.
pub fn[R : @contract.Rng] laplace(r : R, loc~ : Double = 0.0, scale~ : Double = 1.0) -> Double
A Laplace draw: the two-sided exponential, centred on loc, as wide as scale, with variance 2 scale^2.
pub fn[R : @contract.Rng] logistic(r : R, loc~ : Double = 0.0, scale~ : Double = 1.0) -> Double
A logistic draw with location loc and scale scale: the log-odds of one uniform, which is what the distribution is.
pub fn[R : @contract.Rng] lognormvariate(r : R, mu~ : Double = 0.0, sigma~ : Double = 1.0) -> Double
A lognormal draw: exp(gauss(mu, sigma)).
pub fn[R : @contract.Rng] normalvariate(r : R, mu~ : Double = 0.0, sigma~ : Double = 1.0) -> Double
A normal draw by the ratio-of-uniforms method of Kinderman and Monahan, the one Python's normalvariate uses — a different algorithm from gauss, and a different stream for the same seed.
pub fn[R : @contract.Rng] paretovariate(r : R, alpha : Double) -> Double
A Pareto draw with shape alpha: one minus a uniform, raised to -1/alpha.
pub fn[R : @contract.Rng] permutation(r : R, n : Int32) -> Array[Int32]
0 to n - 1 in a random order: the indices of a shuffle, without the array to shuffle. A negative n gives an empty array.
pub fn[R : @contract.Rng] poisson(r : R, mu : Double) -> Int64
A Poisson draw with mean mu: multiplication for small means, and the transformed-rejection method with a squeeze for large ones, so that a mean of a million costs the same as a mean of a hundred.
pub fn[R : @contract.Rng] randbytes(r : R, n : Int32) -> Bytes
n bytes from the stream, the way fill writes them into an array you already have. A negative count gives an empty Bytes.
pub fn[R : @contract.Rng] randint(r : R, lo : Int64, hi : Int64) -> Int64
A uniform integer in the closed interval [lo, hi], unbiased.
pub fn[R : @contract.Rng] random(r : R) -> Double
A uniform Double in [0, 1), with 53 bits of resolution, as Python's random.random draws it.
pub fn[R : @contract.Rng] randrange(r : R, stop : Int64, start~ : Int64 = 0, step? : Int64) -> Int64
A uniform integer from range(start, stop, step), as Python's randrange. stop is required; start defaults to 0 and step to 1.
pub fn[R : @contract.Rng] rayleigh(r : R, scale~ : Double = 1.0) -> Double
A Rayleigh draw with scale scale: the length of a two-dimensional normal vector whose components have that standard deviation.
pub fn[R : @contract.Rng, T] sample(r : R, items : ArrayView[T], k : Int32, weights? : ArrayView[Double]) -> Array[T]
k distinct elements, by a Fisher-Yates partial shuffle. With weights, each place is filled from what is still left, in proportion to the weight of each remaining element, so a heavier element tends to come out earlier.
pub fn seeded(BytesView) -> @chacha.ChaCha12
Start from thirty-two bytes. The same bytes give the same stream, bit for bit on all four backends.
pub fn[R : @contract.Rng, T] shuffle(r : R, a : Array[T]) -> Unit
Shuffle a in place, Fisher-Yates.
pub fn[R : @contract.Rng] student_t(r : R, nu : Double) -> Double
A Student's t draw with nu degrees of freedom: a normal draw over the square root of a chi-squared draw divided by its own degrees of freedom.
pub fn[R : @contract.Rng] triangular(r : R, low~ : Double = 0.0, high~ : Double = 1.0, mode? : Double) -> Double
A triangular draw between low and high with the peak at mode, which defaults to the midpoint. The inverse of the triangular CDF, folded so that one uniform does the work.
pub fn[R : @contract.Rng] vonmisesvariate(r : R, mu : Double, kappa : Double) -> Double
A von Mises draw with mean angle mu and concentration kappa, by the Best-Fisher rejection scheme Python's vonmisesvariate uses. Angles come back in [0, 2 pi).
pub fn[R : @contract.Rng] weibullvariate(r : R, alpha : Double, beta : Double) -> Double
A Weibull draw with scale alpha and shape beta, as Python's weibullvariate names them.