fastmath.random
Various random and noise functions.
Namespace defines various random number generators (RNGs), different types of random functions, sequence generators and noise functions.
RNGs
You can use a selection of various RNGs defined in Apache Commons Math library.
Currently supported RNGs:
:jdk
- default java.util.Random:mersenne
- MersenneTwister:isaac
- ISAAC:well512a
,:well1024a
,:well19937a
,:well19937c
,:well44497a
,:well44497b
- several WELL variants
To create your RNG use rng multimethod. Pass RNG name and (optional) seed. Returned RNG is equipped with RNGProto protocol with methods: irandom, lrandom, frandom drandom, grandom, brandom which return random primitive value with given RNG.
(let [rng (rng :isaac 1337)]
(irandom rng))
For conveniency default RNG (:jdk
) with following functions are created: irand, lrand, frand, drand, grand, brand.
Each prefix denotes returned type:
- i - int
- l - long
- f - float
- d - double
- g - gaussian (double)
- b - boolean
Check individual function for parameters description.
Random Vector Sequences
Couple of functions to generate sequences of numbers or vectors.
To create generator call sequence-generator with generator name and vector size. Following generators are available:
:halton
- Halton low-discrepancy sequence; range [0,1]:sobol
- Sobol low-discrepancy sequence; range [0,1]:r2
- R2 low-discrepancy sequence; range [0,1], moreā¦:sphere
- uniformly random distributed on unit sphere:gaussian
- gaussian distributed (mean=0, stddev=1):default
- uniformly random; range:[0,1]
:halton
, :sobol
and :r2
can be also randomly jittered according to this article. Call jittered-sequence-generator.
After creation you get lazy sequence
Noise
List of continuous noise functions (1d, 2d and 3d):
:value
- value noise:gradient
- gradient noise (improved Ken Perlin version):simplex
- simplex noise
First two (:value
and :gradient
) can use 4 different interpolation types: :none
, :linear
, :hermite
(cubic) and :quintic
.
All can be combined in following variants:
- Noise - pure noise value, create with single-noise
- FBM - fractal brownian motion, create with fbm-noise
- Billow - billow noise, billow-noise
- RidgedMulti - ridged multi, ridgedmulti-noise
Noise creation requires detailed configuration which is simple map of following keys:
:seed
- seed as integer:noise-type
- type of noise::value
,:gradient
(default),:simplex
:interpolation
- type of interpolation (for value and gradient)::none
,:linear
,:hermite
(default) or:quintic
:octaves
- number of octaves for combined noise (like FBM), default: 6:lacunarity
- scaling factor for combined noise, default: 2.00:gain
- amplitude scaling factor for combined noise, default: 0.5:normalize?
- should be normalized to[0,1]
range (true, default) or to[-1,1]
range (false)
For usage convenience 3 ready to use functions are prepared. Returning value from [0,1]
range:
- noise - Perlin Noise (gradient noise, 6 octaves, quintic interpolation)
- vnoise - Value Noise (as in Processing, 6 octaves, hermite interpolation)
- simplex - Simplex Noise (6 octaves)
For random noise generation you can use random-noise-cfg and random-noise-fn. Both can be feed with configuration. Additional configuration:
:generator
can be set to one of the noise variants, defaults to:fbm
:warp-scale
- 0.0 - do not warp, >0.0 warp:warp-depth
- depth for warp (default 1.0, if warp-scale is positive)
Discrete Noise
discrete-noise is a 1d or 2d hash function for given integers. Returns double from [0,1]
range.
Distribution
Various real and integer distributions. See DistributionProto and RNGProto for functions.
To create distribution call distribution multimethod with name as a keyword and map as parameters.
Categories
- Distributions: cdf continuous? covariance default-normal dimensions distribution distribution-id distribution-parameters distributions-list icdf likelihood log-likelihood lower-bound lpdf mean means observe observe1 pdf probability sample source-object upper-bound variance
- Random sequence generation: sequence-generator sequence-generators-list
- Noise functions: billow-noise discrete-noise fbm-noise noise noise-generators noise-interpolations noise-types random-noise-cfg random-noise-fn ridgedmulti-noise simplex single-noise vnoise warp-noise-fn
- Random number generation: ->seq brand brandom default-rng drand drandom flip flipb frand frandom grand grandom irand irandom lrand lrandom randval rng rngs-list set-seed! synced-rng
Other vars: jittered-sequence-generator
->seq
(->seq rng)
(->seq rng n)
Returns lazy sequence of random samples (can be limited to optional n
values).
Examples
Sequence of random values from distribution
(->seq (distribution :gamma) 5)
;;=> (3.5656586756068793
;;=> 4.183711137226517
;;=> 4.867611475289719
;;=> 2.8127240034882934
;;=> 3.6049999735181344)
billow-noise
(billow-noise)
(billow-noise cfg__14990__auto__)
Create billow noise function with optional configuration.
Examples
Usage
(let [n (billow-noise {:seed 12345, :interpolation :none})]
(n 0.5 1.1 -1.3))
;;=> 0.16031746031746036
2d noise
![](images/n/billow.jpg)
brand
Random boolean with default RNG.
Returns true or false with equal probability. You can set p
probability for true
Examples
Usage
(brand)
;;=> true
(brand 0.1)
;;=> false
Count number of
true
values with probability 0.15
(count (filter true? (repeatedly 100000 (fn* [] (brand 0.15)))))
;;=> 14969
brandom
(brandom rng)
(brandom rng p)
Random boolean with provided RNG
Examples
boolean
(rngproto-snippet brandom ...)
;;=> true
cdf
(cdf d v)
(cdf d v1 v2)
Cumulative probability.
Examples
Usage
(cdf (distribution :gamma) 1)
;;=> 0.09020401043104985
(cdf (distribution :gamma) 1 4)
;;=> 0.5037901398591113
continuous?
(continuous? d)
Does distribution support continuous domain?
Examples
Usage
(continuous? (distribution :gamma))
;;=> true
(continuous? (distribution :pascal))
;;=> false
;; Test: ok.
covariance
(covariance d)
Distribution covariance matrix (for multivariate distributions)
Examples
Usage
(covariance (distribution :multi-normal))
;;=> ((1.0 0.0) (0.0 1.0))
(covariance (distribution :dirichlet {:alpha [2 2]}))
;;=> [[0.05 -0.05] [-0.05 0.05]]
;; Test: ok.
default-normal
Default normal distribution (u=0.0, sigma=1.0).
Examples
Usage
(sample default-normal)
;;=> -0.3805445381799295
(set-seed! default-normal 1234)
;;=> org.apache.commons.math3.distribution.NormalDistribution@6f74e852
(sample default-normal)
;;=> 0.14115907833078006
(irandom default-normal)
;;=> 0
(mean default-normal)
;;=> 0.0
(variance default-normal)
;;=> 1.0
default-rng
Default RNG - JDK
Examples
Usage
(set-seed! default-rng 111)
;;=> org.apache.commons.math3.random.JDKRandomGenerator@10732713
(irandom default-rng)
;;=> -1641157356
(set-seed! default-rng 999)
;;=> org.apache.commons.math3.random.JDKRandomGenerator@10732713
(irandom default-rng)
;;=> -421961713
(set-seed! default-rng 111)
;;=> org.apache.commons.math3.random.JDKRandomGenerator@10732713
(irandom default-rng)
;;=> -1641157356
dimensions
(dimensions d)
Distribution dimensionality
Examples
Usage
(dimensions (distribution :gamma))
;;=> 1
(dimensions (distribution :dirichlet {:alpha (repeat 30 2.0)}))
;;=> 30
;; Test: ok.
discrete-noise
(discrete-noise X Y)
(discrete-noise X)
Discrete noise. Parameters:
- X (long)
- Y (long, optional)
Returns double value from [0,1] range
Examples
Example calls
(discrete-noise 123 444)
;;=> 0.8660251823561383
(discrete-noise 123 444)
;;=> 0.8660251823561383
(discrete-noise 123 445)
;;=> 0.4702831345937602
(discrete-noise 123)
;;=> 0.28831296287864117
Draw noise for [0-180] range.
![](images/n/discrete_noise.jpg)
distribution
multimethod
Create distribution object.
- First parameter is distribution as a
:key
. - Second parameter is a map with configuration.
All distributions accept rng
under :rng
key (default: default-rng) and some of them accept inverse-cumm-accuracy
(default set to 1e-9
).
Examples
Usage
(distribution :beta)
;;=> org.apache.commons.math3.distribution.BetaDistribution@2c257485
(distribution :beta {:alpha 1.0, :beta 1.0})
;;=> org.apache.commons.math3.distribution.BetaDistribution@8891aa8
All parameters
(into (sorted-map)
(map (fn* [p1__22237#]
(vector p1__22237#
(sort (distribution-parameters (distribution
p1__22237#)))))
(keys (methods distribution))))
;;=> {:anderson-darling (:n),
;;=> :bernoulli (:p :trials),
;;=> :beta (:alpha :beta),
;;=> :binomial (:p :trials),
;;=> :categorical-distribution (:data :probabilities),
;;=> :cauchy (:median :scale),
;;=> :chi (:nu),
;;=> :chi-squared (:degrees-of-freedom),
;;=> :chi-squared-noncentral (:lambda :nu),
;;=> :continuous-distribution (:bin-count :data :h :kernel :probabilities),
;;=> :cramer-von-mises (:n),
;;=> :dirichlet (:alpha),
;;=> :empirical (:bin-count :data),
;;=> :enumerated-int (:data :probabilities),
;;=> :enumerated-real (:data :probabilities),
;;=> :erlang (:k :lambda),
;;=> :exponential (:mean),
;;=> :f (:denominator-degrees-of-freedom :numerator-degrees-of-freedom),
;;=> :fatigue-life (:beta :gamma :mu),
;;=> :folded-normal (:mu :sigma),
;;=> :frechet (:alpha :beta :delta),
;;=> :gamma (:scale :shape),
;;=> :geometric (:p),
;;=> :gumbel (:beta :mu),
;;=> :half-cauchy (:scale),
;;=> :hyperbolic-secant (:mu :sigma),
;;=> :hypergeometric (:number-of-successes :population-size :sample-size),
;;=> :hypoexponential (:lambdas),
;;=> :hypoexponential-equal (:h :k :n),
;;=> :integer-discrete-distribution (:data :probabilities),
;;=> :inverse-gamma (:alpha :beta),
;;=> :inverse-gaussian (:lambda :mu),
;;=> :johnson-sb (:delta :gamma :lambda :xi),
;;=> :johnson-sl (:delta :gamma :lambda :xi),
;;=> :johnson-su (:delta :gamma :lambda :xi),
;;=> :kolmogorov-smirnov (:n),
;;=> :kolmogorov-smirnov+ (:n),
;;=> :laplace (:beta :mu),
;;=> :levy (:c :mu),
;;=> :log-logistic (:alpha :beta),
;;=> :log-normal (:scale :shape),
;;=> :logarithmic (:theta),
;;=> :logistic (:mu :s),
;;=> :multi-normal (:covariances :means),
;;=> :nakagami (:mu :omega),
;;=> :negative-binomial (:p :r),
;;=> :normal (:mu :sd),
;;=> :normal-inverse-gaussian (:alpha :beta :delta :mu),
;;=> :pareto (:scale :shape),
;;=> :pascal (:p :r),
;;=> :pearson-6 (:alpha1 :alpha2 :beta),
;;=> :poisson (:epsilon :max-iterations :p),
;;=> :power (:a :b :c),
;;=> :rayleigh (:a :beta),
;;=> :real-discrete-distribution (:data :probabilities),
;;=> :reciprocal-sqrt (:a),
;;=> :t (:degrees-of-freedom),
;;=> :triangular (:a :b :c),
;;=> :uniform-int (:lower :upper),
;;=> :uniform-real (:lower :upper),
;;=> :watson-g (:n),
;;=> :watson-u (:n),
;;=> :weibull (:alpha :beta),
;;=> :zipf (:exponent :number-of-elements)}
PDFs of anderson-darling
![](images/d/pdf-anderson-darling.jpg)
CDFs of anderson-darling
![](images/d/cdf-anderson-darling.jpg)
ICDFs of anderson-darling
![](images/d/icdf-anderson-darling.jpg)
PDFs of bernoulli
![](images/d/pdf-bernoulli.jpg)
CDFs of bernoulli
![](images/d/cdf-bernoulli.jpg)
ICDFs of bernoulli
![](images/d/icdf-bernoulli.jpg)
PDFs of beta
![](images/d/pdf-beta.jpg)
CDFs of beta
![](images/d/cdf-beta.jpg)
ICDFs of beta
![](images/d/icdf-beta.jpg)
PDFs of binomial
![](images/d/pdf-binomial.jpg)
CDFs of binomial
![](images/d/cdf-binomial.jpg)
ICDFs of binomial
![](images/d/icdf-binomial.jpg)
PDFs of cauchy
![](images/d/pdf-cauchy.jpg)
CDFs of cauchy
![](images/d/cdf-cauchy.jpg)
ICDFs of cauchy
![](images/d/icdf-cauchy.jpg)
PDFs of chi
![](images/d/pdf-chi.jpg)
CDFs of chi
![](images/d/cdf-chi.jpg)
ICDFs of chi
![](images/d/icdf-chi.jpg)
PDFs of chi-squared
![](images/d/pdf-chi-squared.jpg)
CDFs of chi-squared
![](images/d/cdf-chi-squared.jpg)
ICDFs of chi-squared
![](images/d/icdf-chi-squared.jpg)
PDFs of chi-squared-noncentral
![](images/d/pdf-chi-squared-noncentral.jpg)
CDFs of chi-squared-noncentral
![](images/d/cdf-chi-squared-noncentral.jpg)
ICDFs of chi-squared-noncentral
![](images/d/icdf-chi-squared-noncentral.jpg)
PDFs of continuous-distribution
![](images/d/pdf-continuous-distribution.jpg)
CDFs of continuous-distribution
![](images/d/cdf-continuous-distribution.jpg)
ICDFs of continuous-distribution
![](images/d/icdf-continuous-distribution.jpg)
PDFs of empirical
![](images/d/pdf-empirical.jpg)
CDFs of empirical
![](images/d/cdf-empirical.jpg)
ICDFs of empirical
![](images/d/icdf-empirical.jpg)
PDFs of enumerated-int
![](images/d/pdf-enumerated-int.jpg)
CDFs of enumerated-int
![](images/d/cdf-enumerated-int.jpg)
ICDFs of enumerated-int
![](images/d/icdf-enumerated-int.jpg)
PDFs of enumerated-real
![](images/d/pdf-enumerated-real.jpg)
CDFs of enumerated-real
![](images/d/cdf-enumerated-real.jpg)
ICDFs of enumerated-real
![](images/d/icdf-enumerated-real.jpg)
PDFs of erlang
![](images/d/pdf-erlang.jpg)
CDFs of erlang
![](images/d/cdf-erlang.jpg)
ICDFs of erlang
![](images/d/icdf-erlang.jpg)
PDFs of exponential
![](images/d/pdf-exponential.jpg)
CDFs of exponential
![](images/d/cdf-exponential.jpg)
ICDFs of exponential
![](images/d/icdf-exponential.jpg)
PDFs of f
![](images/d/pdf-f.jpg)
CDFs of f
![](images/d/cdf-f.jpg)
ICDFs of f
![](images/d/icdf-f.jpg)
PDFs of fatigue-life
![](images/d/pdf-fatigue-life.jpg)
CDFs of fatigue-life
![](images/d/cdf-fatigue-life.jpg)
ICDFs of fatigue-life
![](images/d/icdf-fatigue-life.jpg)
PDFs of folded-normal
![](images/d/pdf-folded-normal.jpg)
CDFs of folded-normal
![](images/d/cdf-folded-normal.jpg)
ICDFs of folded-normal
![](images/d/icdf-folded-normal.jpg)
PDFs of frechet
![](images/d/pdf-frechet.jpg)
CDFs of frechet
![](images/d/cdf-frechet.jpg)
ICDFs of frechet
![](images/d/icdf-frechet.jpg)
PDFs of gamma
![](images/d/pdf-gamma.jpg)
CDFs of gamma
![](images/d/cdf-gamma.jpg)
ICDFs of gamma
![](images/d/icdf-gamma.jpg)
PDFs of geometric
![](images/d/pdf-geometric.jpg)
CDFs of geometric
![](images/d/cdf-geometric.jpg)
ICDFs of geometric
![](images/d/icdf-geometric.jpg)
PDFs of gumbel
![](images/d/pdf-gumbel.jpg)
CDFs of gumbel
![](images/d/cdf-gumbel.jpg)
ICDFs of gumbel
![](images/d/icdf-gumbel.jpg)
PDFs of half-cauchy
![](images/d/pdf-half-cauchy.jpg)
CDFs of half-cauchy
![](images/d/cdf-half-cauchy.jpg)
ICDFs of half-cauchy
![](images/d/icdf-half-cauchy.jpg)
PDFs of hyperbolic-secant
![](images/d/pdf-hyperbolic-secant.jpg)
CDFs of hyperbolic-secant
![](images/d/cdf-hyperbolic-secant.jpg)
ICDFs of hyperbolic-secant
![](images/d/icdf-hyperbolic-secant.jpg)
PDFs of hypergeometric
![](images/d/pdf-hypergeometric.jpg)
CDFs of hypergeometric
![](images/d/cdf-hypergeometric.jpg)
ICDFs of hypergeometric
![](images/d/icdf-hypergeometric.jpg)
PDFs of hypoexponential
![](images/d/pdf-hypoexponential.jpg)
CDFs of hypoexponential
![](images/d/cdf-hypoexponential.jpg)
ICDFs of hypoexponential
![](images/d/icdf-hypoexponential.jpg)
PDFs of hypoexponential-equal
![](images/d/pdf-hypoexponential-equal.jpg)
CDFs of hypoexponential-equal
![](images/d/cdf-hypoexponential-equal.jpg)
ICDFs of hypoexponential-equal
![](images/d/icdf-hypoexponential-equal.jpg)
PDFs of integer-discrete-distribution
![](images/d/pdf-integer-discrete-distribution.jpg)
CDFs of integer-discrete-distribution
![](images/d/cdf-integer-discrete-distribution.jpg)
ICDFs of integer-discrete-distribution
![](images/d/icdf-integer-discrete-distribution.jpg)
PDFs of inverse-gamma
![](images/d/pdf-inverse-gamma.jpg)
CDFs of inverse-gamma
![](images/d/cdf-inverse-gamma.jpg)
ICDFs of inverse-gamma
![](images/d/icdf-inverse-gamma.jpg)
PDFs of inverse-gaussian
![](images/d/pdf-inverse-gaussian.jpg)
CDFs of inverse-gaussian
![](images/d/cdf-inverse-gaussian.jpg)
ICDFs of inverse-gaussian
![](images/d/icdf-inverse-gaussian.jpg)
PDFs of johnson-sb
![](images/d/pdf-johnson-sb.jpg)
CDFs of johnson-sb
![](images/d/cdf-johnson-sb.jpg)
ICDFs of johnson-sb
![](images/d/icdf-johnson-sb.jpg)
PDFs of johnson-sl
![](images/d/pdf-johnson-sl.jpg)
CDFs of johnson-sl
![](images/d/cdf-johnson-sl.jpg)
ICDFs of johnson-sl
![](images/d/icdf-johnson-sl.jpg)
PDFs of johnson-su
![](images/d/pdf-johnson-su.jpg)
CDFs of johnson-su
![](images/d/cdf-johnson-su.jpg)
ICDFs of johnson-su
![](images/d/icdf-johnson-su.jpg)
PDFs of kolmogorov-smirnov
![](images/d/pdf-kolmogorov-smirnov.jpg)
CDFs of kolmogorov-smirnov
![](images/d/cdf-kolmogorov-smirnov.jpg)
ICDFs of kolmogorov-smirnov
![](images/d/icdf-kolmogorov-smirnov.jpg)
PDFs of kolmogorov-smirnov+
![](images/d/pdf-kolmogorov-smirnov+.jpg)
CDFs of kolmogorov-smirnov+
![](images/d/cdf-kolmogorov-smirnov+.jpg)
ICDFs of kolmogorov-smirnov+
![](images/d/icdf-kolmogorov-smirnov+.jpg)
PDFs of laplace
![](images/d/pdf-laplace.jpg)
CDFs of laplace
![](images/d/cdf-laplace.jpg)
ICDFs of laplace
![](images/d/icdf-laplace.jpg)
PDFs of levy
![](images/d/pdf-levy.jpg)
CDFs of levy
![](images/d/cdf-levy.jpg)
ICDFs of levy
![](images/d/icdf-levy.jpg)
PDFs of log-logistic
![](images/d/pdf-log-logistic.jpg)
CDFs of log-logistic
![](images/d/cdf-log-logistic.jpg)
ICDFs of log-logistic
![](images/d/icdf-log-logistic.jpg)
PDFs of log-normal
![](images/d/pdf-log-normal.jpg)
CDFs of log-normal
![](images/d/cdf-log-normal.jpg)
ICDFs of log-normal
![](images/d/icdf-log-normal.jpg)
PDFs of logistic
![](images/d/pdf-logistic.jpg)
CDFs of logistic
![](images/d/cdf-logistic.jpg)
ICDFs of logistic
![](images/d/icdf-logistic.jpg)
PDFs of nakagami
![](images/d/pdf-nakagami.jpg)
CDFs of nakagami
![](images/d/cdf-nakagami.jpg)
ICDFs of nakagami
![](images/d/icdf-nakagami.jpg)
PDFs of negative-binomial
![](images/d/pdf-negative-binomial.jpg)
CDFs of negative-binomial
![](images/d/cdf-negative-binomial.jpg)
ICDFs of negative-binomial
![](images/d/icdf-negative-binomial.jpg)
PDFs of normal
![](images/d/pdf-normal.jpg)
CDFs of normal
![](images/d/cdf-normal.jpg)
ICDFs of normal
![](images/d/icdf-normal.jpg)
PDFs of pareto
![](images/d/pdf-pareto.jpg)
CDFs of pareto
![](images/d/cdf-pareto.jpg)
ICDFs of pareto
![](images/d/icdf-pareto.jpg)
PDFs of pascal
![](images/d/pdf-pascal.jpg)
CDFs of pascal
![](images/d/cdf-pascal.jpg)
ICDFs of pascal
![](images/d/icdf-pascal.jpg)
PDFs of pearson-6
![](images/d/pdf-pearson-6.jpg)
CDFs of pearson-6
![](images/d/cdf-pearson-6.jpg)
ICDFs of pearson-6
![](images/d/icdf-pearson-6.jpg)
PDFs of poisson
![](images/d/pdf-poisson.jpg)
CDFs of poisson
![](images/d/cdf-poisson.jpg)
ICDFs of poisson
![](images/d/icdf-poisson.jpg)
PDFs of power
![](images/d/pdf-power.jpg)
CDFs of power
![](images/d/cdf-power.jpg)
ICDFs of power
![](images/d/icdf-power.jpg)
PDFs of rayleigh
![](images/d/pdf-rayleigh.jpg)
CDFs of rayleigh
![](images/d/cdf-rayleigh.jpg)
ICDFs of rayleigh
![](images/d/icdf-rayleigh.jpg)
PDFs of real-discrete-distribution
![](images/d/pdf-real-discrete-distribution.jpg)
CDFs of real-discrete-distribution
![](images/d/cdf-real-discrete-distribution.jpg)
ICDFs of real-discrete-distribution
![](images/d/icdf-real-discrete-distribution.jpg)
PDFs of reciprocal-sqrt
![](images/d/pdf-reciprocal-sqrt.jpg)
CDFs of reciprocal-sqrt
![](images/d/cdf-reciprocal-sqrt.jpg)
ICDFs of reciprocal-sqrt
![](images/d/icdf-reciprocal-sqrt.jpg)
PDFs of t
![](images/d/pdf-t.jpg)
CDFs of t
![](images/d/cdf-t.jpg)
ICDFs of t
![](images/d/icdf-t.jpg)
PDFs of triangular
![](images/d/pdf-triangular.jpg)
CDFs of triangular
![](images/d/cdf-triangular.jpg)
ICDFs of triangular
![](images/d/icdf-triangular.jpg)
PDFs of uniform-int
![](images/d/pdf-uniform-int.jpg)
CDFs of uniform-int
![](images/d/cdf-uniform-int.jpg)
ICDFs of uniform-int
![](images/d/icdf-uniform-int.jpg)
PDFs of uniform-real
![](images/d/pdf-uniform-real.jpg)
CDFs of uniform-real
![](images/d/cdf-uniform-real.jpg)
ICDFs of uniform-real
![](images/d/icdf-uniform-real.jpg)
PDFs of watson-g
![](images/d/pdf-watson-g.jpg)
CDFs of watson-g
![](images/d/cdf-watson-g.jpg)
ICDFs of watson-g
![](images/d/icdf-watson-g.jpg)
PDFs of watson-u
![](images/d/pdf-watson-u.jpg)
CDFs of watson-u
![](images/d/cdf-watson-u.jpg)
ICDFs of watson-u
![](images/d/icdf-watson-u.jpg)
PDFs of weibull
![](images/d/pdf-weibull.jpg)
CDFs of weibull
![](images/d/cdf-weibull.jpg)
ICDFs of weibull
![](images/d/icdf-weibull.jpg)
PDFs of zipf
![](images/d/pdf-zipf.jpg)
CDFs of zipf
![](images/d/cdf-zipf.jpg)
ICDFs of zipf
![](images/d/icdf-zipf.jpg)
2d multidimensional normal (mean=[0,0], covariances=I)
![](images/d/multi-normal.jpg)
2d multidimensional normal (mean=[0,0], covariances=1 -1] [-1 2)
![](images/d/multi-normal2.jpg)
2d dirichlet (alpha=[2,0.8])
![](images/d/dirichlet.jpg)
distribution-id
(distribution-id d)
Distribution identifier as keyword.
Examples
Usage
(distribution-id (distribution :gamma))
;;=> :gamma
(distribution-id default-normal)
;;=> :normal
;; Test: ok.
distribution-parameters
(distribution-parameters d)
(distribution-parameters d all?)
Distribution highest supported value.
When all?
is true, technical parameters are included, ie: :rng
and :inverser-cumm-accuracy
.
Examples
Usage
(distribution-parameters (distribution :gamma))
;;=> [:scale :shape]
(distribution-parameters (distribution :gamma) true)
;;=> [:rng :shape :scale :inverse-cumm-accuracy]
(distribution-parameters default-normal)
;;=> [:sd :mu]
;; Test: ok.
distributions-list
List of distributions.
Examples
Number and list of distributions
distributions-list
;;=> #{:anderson-darling :bernoulli :beta :binomial :categorical-distribution
;;=> :cauchy :chi :chi-squared :chi-squared-noncentral
;;=> :continuous-distribution :cramer-von-mises :dirichlet :empirical
;;=> :enumerated-int :enumerated-real :erlang :exponential :f :fatigue-life
;;=> :folded-normal :frechet :gamma :geometric :gumbel :half-cauchy
;;=> :hyperbolic-secant :hypergeometric :hypoexponential
;;=> :hypoexponential-equal :integer-discrete-distribution :inverse-gamma
;;=> :inverse-gaussian :johnson-sb :johnson-sl :johnson-su
;;=> :kolmogorov-smirnov :kolmogorov-smirnov+ :laplace :levy :log-logistic
;;=> :log-normal :logarithmic :logistic :multi-normal :nakagami
;;=> :negative-binomial :normal :normal-inverse-gaussian :pareto :pascal
;;=> :pearson-6 :poisson :power :rayleigh :real-discrete-distribution
;;=> :reciprocal-sqrt :t :triangular :uniform-int :uniform-real :watson-g
;;=> :watson-u :weibull :zipf}
(count distributions-list)
;;=> 64
drand
(drand)
(drand mx)
(drand mn mx)
Random double number with default RNG.
As default returns random double from [0,1)
range. When mx
is passed, range is set to [0, mx)
. When mn
is passed, range is set to [mn, mx)
.
Examples
Usage
(drand)
;;=> 0.38178681319155416
(drand 10)
;;=> 6.592415290746953
(drand 10 20)
;;=> 12.544011338963232
drandom
(drandom rng)
(drandom rng mx)
(drandom rng mn mx)
Random double number with provided RNG
Examples
double
(rngproto-snippet drandom ...)
;;=> 0.6451540636325555
Double random value from distribution
(drandom (distribution :gamma))
;;=> 1.3987098491779502
fbm-noise
(fbm-noise)
(fbm-noise cfg__14990__auto__)
Create fbm noise function with optional configuration.
Examples
Usage
(let [n (fbm-noise {:interpolation :linear, :noise-type :value})]
(n 0.5 1.1 -1.3))
;;=> 0.5565531220960567
2d noise
![](images/n/fbm.jpg)
flip
(flip p)
(flip)
Returns 1 with given probability, 0 otherwise
Examples
Usage
(flip)
;;=> 1
(flip 0.2)
;;=> 0
(repeatedly 10 (fn* [] (flip 0.1)))
;;=> (0 0 0 0 0 0 0 0 0 0)
flipb
(flipb p)
(flipb)
Returns true with given probability, false otherwise
Examples
Usage
(flipb)
;;=> true
(flipb 0.2)
;;=> true
(repeatedly 10 (fn* [] (flipb 0.1)))
;;=> (false false false false false true true false false false)
frand
(frand)
(frand mx)
(frand mn mx)
Random double number with default RNG.
As default returns random float from [0,1)
range. When mx
is passed, range is set to [0, mx)
. When mn
is passed, range is set to [mn, mx)
.
Examples
Usage
(frand)
;;=> 0.288085013628006
(frand 10)
;;=> 6.10488748550415
(frand 10 20)
;;=> 18.232948303222656
frandom
(frandom rng)
(frandom rng mx)
(frandom rng mn mx)
Random double number with provided RNG
Examples
float
(rngproto-snippet frandom ...)
;;=> 0.8122085332870483
Float random value from distribution (sample cast to
float
)
(frandom (distribution :gamma))
;;=> 3.709850311279297
grand
(grand)
(grand stddev)
(grand mean stddev)
Random gaussian double number with default RNG.
As default returns random double from N(0,1)
. When std
is passed, N(0,std)
is used. When mean
is passed, distribution is set to N(mean, std)
.
Examples
Usage
(grand)
;;=> 0.11619737635740301
(grand 10)
;;=> 1.1210684816151368
(grand 10 20)
;;=> 17.305779775324304
grandom
(grandom rng)
(grandom rng stddev)
(grandom rng mean stddev)
Random gaussian double number with provided RNG
Examples
gaussian double
(rngproto-snippet grandom ...)
;;=> -0.5076621677752612
icdf
(icdf d v)
Inverse cumulative probability
Examples
Usage
(icdf (distribution :gamma) 0.5)
;;=> 3.3566939800333233
irand
(irand)
(irand mx)
(irand mn mx)
Random integer number with default RNG.
As default returns random integer from full integer range. When mx
is passed, range is set to [0, mx)
. When mn
is passed, range is set to [mn, mx)
.
Examples
Usage
(irand)
;;=> 1034032361
(irand 10)
;;=> 9
(irand 10 20)
;;=> 16
irandom
(irandom rng)
(irandom rng mx)
(irandom rng mn mx)
Random integer number with provided RNG
Examples
integer
(rngproto-snippet irandom ...)
;;=> 154677565
Integer random value from distribution (sample cast to
int
)
(irandom (distribution :gamma))
;;=> 3
jittered-sequence-generator
(jittered-sequence-generator seq-generator dimensions)
(jittered-sequence-generator seq-generator dimensions jitter)
Create jittered sequence generator.
Suitable for :r2
, :sobol
and :halton
sequences.
jitter
parameter range is from 0
(no jitter) to 1
(full jitter). Default: 0.25.
See also sequence-generator.
Examples
Usage
(let [gen1 (jittered-sequence-generator :r2 2 0.5)
gen2 (jittered-sequence-generator :r2 2 0.5)]
[(first gen1) (first gen2)])
;;=> [[0.4613760402754695 0.07709416278300169]
;;=> [0.4119796194930869 0.28702226482504817]]
Jittered (0.5) R2 plot (500 samples)
![](images/r/jr2.jpg)
Jittered (0.5) Halton plot (500 samples)
![](images/r/jhalton.jpg)
Jittered (0.5) Sobol plot (500 samples)
![](images/r/jsobol.jpg)
Jittered (0.5) Sphere plot (500 samples)
![](images/r/jsphere.jpg)
Jittered (0.5) Gaussian plot (500 samples)
![](images/r/jgaussian.jpg)
Jittered (0.5) Default plot (500 samples)
![](images/r/jdefault.jpg)
likelihood
(likelihood d vs)
Likelihood of samples
Examples
Usage
(likelihood (distribution :gamma) [10 0.5 0.5 1 2])
;;=> 4.452548659934162E-6
log-likelihood
(log-likelihood d vs)
Log likelihood of samples
Examples
Usage
(log-likelihood (distribution :gamma) [10 0.5 0.5 1 2])
;;=> -12.322033893165353
lower-bound
(lower-bound d)
Distribution lowest supported value
Examples
Usage
(lower-bound (distribution :gamma))
;;=> 0.0
;; Test: ok.
lpdf
(lpdf d v)
Log density
Examples
Usage
(lpdf (distribution :gamma) 1)
;;=> -1.8862943611198908
lrand
(lrand)
(lrand mx)
(lrand mn mx)
Random long number with default RNG.
As default returns random long from full integer range. When mx
is passed, range is set to [0, mx)
. When mn
is passed, range is set to [mn, mx)
.
Examples
Usage
(lrand)
;;=> 3047173689416362927
(lrand 10)
;;=> 2
(lrand 10 20)
;;=> 14
lrandom
(lrandom rng)
(lrandom rng mx)
(lrandom rng mn mx)
Random long number with provided RNG
Examples
long
(rngproto-snippet lrandom ...)
;;=> -7006375016781005293
Long random value from distribution (sample cast to
long
)
(lrandom (distribution :gamma))
;;=> 0
mean
(mean d)
Distribution mean
Examples
Usage
(mean (distribution :gamma))
;;=> 4.0
;; Test: ok.
means
(means d)
Distribution means (for multivariate distributions)
Examples
Usage
(means (distribution :multi-normal))
;;=> [0.0 0.0]
(means (distribution :dirichlet {:alpha [2 2]}))
;;=> (0.5 0.5)
;; Test: ok.
noise
(noise x)
(noise x y)
(noise x y z)
Improved Perlin Noise.
6 octaves, quintic interpolation.
Examples
Usage
(noise 3.3)
;;=> 0.3792675555555556
(noise 3.3 1.1)
;;=> 0.5979212982044446
(noise 3.3 0.0 -0.1)
;;=> 0.5611104175542858
2d noise
![](images/n/noise.jpg)
noise-generators
List of possible noise generators as a map of names and functions.
Examples
List of names (keys)
(keys noise-generators)
;;=> (:fbm :single :billow :ridgemulti)
noise-interpolations
List of possible noise interpolations as a map of names and values.
Examples
List of names (keys)
(keys noise-interpolations)
;;=> (:none :linear :hermite :quintic)
noise-types
List of possible noise types as a map of names and values.
Examples
List of names (keys)
(keys noise-types)
;;=> (:value :gradient :simplex)
observe
macro
(observe d vs)
Log likelihood of samples. Alias for log-likelihood.
Examples
Usage
(observe (distribution :gamma) [10 0.5 0.5 1 2])
;;=> -12.322033893165353
observe1
(observe1 d v)
Log of probability/density of the value. Alias for lpdf.
Examples
Usage
(observe1 (distribution :gamma) 10)
;;=> -4.083709268125845
(pdf d v)
Density
Examples
Usage
(pdf (distribution :gamma) 1)
;;=> 0.15163266492815838
(pdf (distribution :pascal) 1)
;;=> 0.078125
probability
(probability d v)
Probability (PMF)
Examples
Usage
(probability (distribution :gamma) 1)
;;=> 0.15163266492815838
(probability (distribution :pascal) 1)
;;=> 0.078125
random-noise-cfg
(random-noise-cfg pre-config)
(random-noise-cfg)
Create random noise configuration.
Optional map with fixed values.
Examples
Random configuration
(random-noise-cfg)
;;=> {:gain 0.618855368328213,
;;=> :generator :single,
;;=> :interpolation :none,
;;=> :lacunarity 1.599940053984084,
;;=> :noise-type :gradient,
;;=> :normalize? true,
;;=> :octaves 3,
;;=> :seed -1844158816,
;;=> :warp-depth 2,
;;=> :warp-scale 0.0}
random-noise-fn
(random-noise-fn cfg)
(random-noise-fn)
Create random noise function from all possible options.
Optionally provide own configuration cfg
. In this case one of 4 different blending methods will be selected.
Examples
Create function
(random-noise-fn)
;;=> fastmath.random$single_noise$fn__15000@4ada5f90
(random-noise-fn (random-noise-cfg))
;;=> fastmath.random$ridgedmulti_noise$fn__15012@3e4bca9
One
![](images/n/random1.jpg)
Two
![](images/n/random2.jpg)
Three
![](images/n/random3.jpg)
randval
macro
(randval v1 v2)
(randval prob v1 v2)
(randval prob)
(randval)
Retrun value with given probability (default 0.5)
Examples
Usage
(randval :val-one :val-two)
;;=> :val-one
(randval 0.001 :low-probability :high-probability)
;;=> :high-probability
Check probability of nil (should return value around 1000).
(count (filter nil?
(repeatedly 1000000 (fn* [] (randval 0.001 nil 101)))))
;;=> 965
ridgedmulti-noise
(ridgedmulti-noise)
(ridgedmulti-noise cfg__14990__auto__)
Create ridgedmulti noise function with optional configuration.
Examples
Usage
(let [n
(ridgedmulti-noise
{:octaves 3, :lacunarity 2.1, :gain 0.7, :noise-type :simplex})]
(n 0.5 1.1 -1.3))
;;=> 0.7702044687581495
2d noise
![](images/n/ridgedmulti.jpg)
rng
multimethod
Examples
Creation
(rng :mersenne)
;;=> org.apache.commons.math3.random.MersenneTwister@6a6ce193
(rng :isaac 1234)
;;=> org.apache.commons.math3.random.ISAACRandom@4c4b1983
Usage
(irandom (rng :mersenne 999) 15 25)
;;=> 17
rngs-list
List of all possible RNGs.
Examples
Contains
(sort rngs-list)
;;=> (:isaac :jdk :mersenne
;;=> :well1024a :well19937a
;;=> :well19937c :well44497a
;;=> :well44497b :well512a)
sample
(sample d)
Random sample
Examples
Random value from distribution
(sample (distribution :gamma))
;;=> 2.2970553541786
sequence-generator
multimethod
Create Sequence generator. See sequence-generators-list for names.
Values:
:r2
,:halton
,:sobol
,:default
- range[0-1] for each dimension
:gaussian
- fromN(0,1)
distribution:sphere
- from surface of unit sphere (ie. euclidean distance from origin equals 1.0)
Possible dimensions:
:r2
- 1-15:halton
- 1-40:sobol
- 1-1000- the rest - 1+
See also jittered-sequence-generator.
Examples
Usage (2d)
(let [gen (sequence-generator :halton 2)] (take 5 gen))
;;=> ([0.0 0.0]
;;=> [0.5 0.6666666666666666]
;;=> [0.25 0.3333333333333333]
;;=> [0.75 0.2222222222222222]
;;=> [0.125 0.8888888888888888])
Usage (1d)
(let [gen (sequence-generator :sobol 1)] (take 5 gen))
;;=> (0.0 0.5 0.75 0.25 0.375)
Usage (10d)
(second (sequence-generator :halton 10))
;;=> [0.5 0.6666666666666666 0.6000000000000001 0.42857142857142855
;;=> 0.7272727272727273 0.8461538461538463 0.7058823529411764
;;=> 0.7368421052631579 0.30434782608695654 0.6206896551724138]
Usage, R2 sequence
(take 5 (sequence-generator :r2 3))
;;=> ([0.3191725133961645 0.17104360670378926 0.0497004779019703]
;;=> [0.13834502679232896 0.8420872134075785 0.5994009558039406]
;;=> [0.9575175401884934 0.5131308201113678 0.1491014337059109]
;;=> [0.7766900535846579 0.18417442681515706 0.6988019116078812]
;;=> [0.5958625669808224 0.8552180335189463 0.24850238950985148])
R2 plot (500 samples)
![](images/r/r2.jpg)
Halton plot (500 samples)
![](images/r/halton.jpg)
Sobol plot (500 samples)
![](images/r/sobol.jpg)
Sphere plot (500 samples)
![](images/r/sphere.jpg)
Gaussian plot (500 samples)
![](images/r/gaussian.jpg)
Default plot (500 samples)
![](images/r/default.jpg)
sequence-generators-list
List of random sequence generator. See sequence-generator.
Examples
Generator names.
(sort sequence-generators-list)
;;=> (:default :gaussian :halton :r2 :sobol :sphere)
set-seed!
(set-seed! rng v)
Sets seed. Returns rng
.
Examples
Set seed for the RNG object
(let [rng (rng :isaac)]
(set-seed! rng 1234)
(irandom rng 10 15))
;;=> 10
;; Test: ok.
Set seed for the distribution object
(let [d (distribution :enumerated-int {:data [1 1 1 2 3]})]
(set-seed! d 1234)
(irandom d))
;;=> 2
;; Test: ok.
simplex
(simplex x)
(simplex x y)
(simplex x y z)
Simplex noise. 6 octaves.
Examples
Usage
(simplex 3.3)
;;=> 0.6560218691923807
(simplex 3.3 1.1)
;;=> 0.3940602659610949
(simplex 3.3 0.0 -0.1)
;;=> 0.656340461166634
2d noise
![](images/n/simplex.jpg)
single-noise
(single-noise)
(single-noise cfg__14990__auto__)
Create single noise function with optional configuration.
Examples
Usage
(let [n (single-noise {:interpolation :linear})] (n 0.5 1.1 -1.3))
;;=> 0.3275
2d noise
![](images/n/single.jpg)
source-object
(source-object d)
Returns Java or proxy object from backend library (if available)
Examples
Usage
(source-object default-normal)
;;=> org.apache.commons.math3.distribution.NormalDistribution@6f74e852
synced-rng
(synced-rng m)
(synced-rng m seed)
Create synchronized RNG for given name and optional seed. Wraps rng method.
Examples
Usage
(drandom (synced-rng :mersenne 1234))
;;=> 0.1915194466361949
upper-bound
(upper-bound d)
Distribution highest supported value
Examples
Usage
(upper-bound (distribution :gamma))
;;=> Infinity
;; Test: ok.
variance
(variance d)
Distribution variance
Examples
Usage
(variance (distribution :gamma))
;;=> 8.0
;; Test: ok.
vnoise
(vnoise x)
(vnoise x y)
(vnoise x y z)
Value Noise.
6 octaves, Hermite interpolation (cubic, h01).
Examples
Usage
(vnoise 3.3)
;;=> 0.5459827831802206
(vnoise 3.3 1.1)
;;=> 0.6047808080658774
(vnoise 3.3 0.0 -0.1)
;;=> 0.34661294144531285
2d noise
![](images/n/vnoise.jpg)
warp-noise-fn
(warp-noise-fn noise scale depth)
(warp-noise-fn noise scale)
(warp-noise-fn noise)
(warp-noise-fn)
Create warp noise (see Inigo Quilez article).
Parameters:
- noise function, default: vnoise
- scale factor, default: 4.0
- depth (1 or 2), default 1
Normalization of warp noise depends on normalization of noise function.
Examples
Usage
(let [n (warp-noise-fn simplex 2.0 2.0)]
[(n 0.0) (n 1.0 0.5) (n 2 2 2)])
;;=> [0.28698139304052883 0.3188025978522241 0.45982809810709013]
Default warp (noise=vnoise, scale=4.0, depth=1.0).
![](images/n/warp.jpg)