p-random
The random module provides random number generation, probability distributions, and sequence utilities.
use p-random as rrandom[]
Section titled “random[]”Returns a floating-point number in [0, 1].
use p-random as rconst x be r.random[] -- e.g. 0.7312...randint[a; b]
Section titled “randint[a; b]”Returns a random integer from a to b (inclusive).
use p-random as rr.randint[1; 6] -- e.g. 4 (dice roll)randrange[stop] / randrange[start; stop] / randrange[start; stop; step]
Section titled “randrange[stop] / randrange[start; stop] / randrange[start; stop; step]”Returns a random integer from the range. With one argument, the range is [0, stop). With two, [start, stop). With three, a random element from start, start+step, start+2*step, ... below stop.
use p-random as rr.randrange[10] -- 0..9r.randrange[5; 10] -- 5..9r.randrange[0; 20; 2] -- even number 0..18randbool[] / randbool[p]
Section titled “randbool[] / randbool[p]”Returns true with probability p (default: 0.5).
use p-random as rr.randbool[] -- 50% truer.randbool[0.8] -- 80% truegetrandbits[k]
Section titled “getrandbits[k]”Returns an integer with k random bits.
use p-random as rr.getrandbits[8] -- 0..255r.getrandbits[16] -- 0..65535randbytes[n]
Section titled “randbytes[n]”Returns an array of n random bytes (0–255).
use p-random as rr.randbytes[4] -- e.g. [142, 55, 200, 13]normalvariate[mu; sigma]
Section titled “normalvariate[mu; sigma]”Alias for gauss. Returns a random number from the normal distribution.
Real-Valued Distributions
Section titled “Real-Valued Distributions”uniform[a; b]
Section titled “uniform[a; b]”Returns a random floating-point number in [a, b).
use p-random as rr.uniform[1.0; 5.0] -- e.g. 3.271...triangular[lo; hi; mode]
Section titled “triangular[lo; hi; mode]”Returns a random number from the triangular distribution. All parameters are optional (default: lo=0, hi=1, mode=midpoint).
gauss[mu; sigma]
Section titled “gauss[mu; sigma]”Returns a random number from the Gaussian (normal) distribution with mean mu and standard deviation sigma. Both are optional (default: mu=0, sigma=1).
use p-random as rr.gauss[0; 1] -- standard normalr.gauss[100; 15] -- IQ-like distributionexpovariate[lambd]
Section titled “expovariate[lambd]”Returns a random number from the exponential distribution with rate lambd.
gammavariate[alpha; beta]
Section titled “gammavariate[alpha; beta]”Returns a random number from the gamma distribution.
betavariate[alpha; beta]
Section titled “betavariate[alpha; beta]”Returns a random number from the beta distribution in [0, 1].
lognormvariate[mu; sigma]
Section titled “lognormvariate[mu; sigma]”Returns a random number from the log-normal distribution.
vonmisesvariate[mu; kappa]
Section titled “vonmisesvariate[mu; kappa]”Returns a random angle from the von Mises distribution (circular normal). mu is the mean angle in radians, kappa is the concentration parameter.
paretovariate[alpha]
Section titled “paretovariate[alpha]”Returns a random number from the Pareto distribution.
weibullvariate[alpha; beta]
Section titled “weibullvariate[alpha; beta]”Returns a random number from the Weibull distribution.
Sequence
Section titled “Sequence”choice[arr]
Section titled “choice[arr]”Returns a random element from the array.
use p-random as rr.choice[[///red///; ///green///; ///blue///]] -- e.g. "green"choices[arr; k]
Section titled “choices[arr; k]”Returns an array of k random elements (with replacement).
use p-random as rr.choices[[///a///; ///b///; ///c///]; 5] -- e.g. ["b", "a", "c", "a", "b"]wchoices[arr; weights; k]
Section titled “wchoices[arr; weights; k]”Returns k weighted random selections. Each element’s probability is proportional to its weight.
use p-random as rr.wchoices[[///rare///; ///common///]; [1; 9]; 10]-- mostly "common"shuffle[arr]
Section titled “shuffle[arr]”Returns a new array with elements in random order (does not mutate the original).
use p-random as rr.shuffle[[1, 2, 3, 4, 5]] -- e.g. [3, 1, 5, 2, 4]sample[arr; k]
Section titled “sample[arr; k]”Returns k unique random elements from the array (without replacement).
use p-random as rr.sample[[1, 2, 3, 4, 5]; 3] -- e.g. [4, 1, 3]Discrete Distributions
Section titled “Discrete Distributions”binomial[n; p]
Section titled “binomial[n; p]”Returns a random number from the binomial distribution: the number of successes in n trials with probability p.
use p-random as rr.binomial[10; 0.5] -- e.g. 6poisson[lambda]
Section titled “poisson[lambda]”Returns a random number from the Poisson distribution with expected value lambda.
geometric[p]
Section titled “geometric[p]”Returns from the geometric distribution: the number of trials needed to get the first success with probability p.
Utility
Section titled “Utility”clamp[val; lo; hi]
Section titled “clamp[val; lo; hi]”Returns val clamped to the range [lo, hi].
use p-random as rr.clamp[15; 0; 10] -- 10r.clamp[-5; 0; 10] -- 0r.clamp[5; 0; 10] -- 5lerp[a; b; t]
Section titled “lerp[a; b; t]”Linear interpolation between a and b by factor t (0–1).
use p-random as rr.lerp[0; 100; 0.5] -- 50r.lerp[0; 100; 0.25] -- 25