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
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.
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
CDFs of anderson-darling
ICDFs of anderson-darling
PDFs of bernoulli
CDFs of bernoulli
ICDFs of bernoulli
PDFs of beta
CDFs of beta
ICDFs of beta
PDFs of binomial
CDFs of binomial
ICDFs of binomial
PDFs of cauchy
CDFs of cauchy
ICDFs of cauchy
PDFs of chi
CDFs of chi
ICDFs of chi
PDFs of chi-squared
CDFs of chi-squared
ICDFs of chi-squared
PDFs of chi-squared-noncentral
CDFs of chi-squared-noncentral
ICDFs of chi-squared-noncentral
PDFs of continuous-distribution
CDFs of continuous-distribution
ICDFs of continuous-distribution
PDFs of empirical
CDFs of empirical
ICDFs of empirical
PDFs of enumerated-int
CDFs of enumerated-int
ICDFs of enumerated-int
PDFs of enumerated-real
CDFs of enumerated-real
ICDFs of enumerated-real
PDFs of erlang
CDFs of erlang
ICDFs of erlang
PDFs of exponential
CDFs of exponential
ICDFs of exponential
PDFs of f
CDFs of f
ICDFs of f
PDFs of fatigue-life
CDFs of fatigue-life
ICDFs of fatigue-life
PDFs of folded-normal
CDFs of folded-normal
ICDFs of folded-normal
PDFs of frechet
CDFs of frechet
ICDFs of frechet
PDFs of gamma
CDFs of gamma
ICDFs of gamma
PDFs of geometric
CDFs of geometric
ICDFs of geometric
PDFs of gumbel
CDFs of gumbel
ICDFs of gumbel
PDFs of half-cauchy
CDFs of half-cauchy
ICDFs of half-cauchy
PDFs of hyperbolic-secant
CDFs of hyperbolic-secant
ICDFs of hyperbolic-secant
PDFs of hypergeometric
CDFs of hypergeometric
ICDFs of hypergeometric
PDFs of hypoexponential
CDFs of hypoexponential
ICDFs of hypoexponential
PDFs of hypoexponential-equal
CDFs of hypoexponential-equal
ICDFs of hypoexponential-equal
PDFs of integer-discrete-distribution
CDFs of integer-discrete-distribution
ICDFs of integer-discrete-distribution
PDFs of inverse-gamma
CDFs of inverse-gamma
ICDFs of inverse-gamma
PDFs of inverse-gaussian
CDFs of inverse-gaussian
ICDFs of inverse-gaussian
PDFs of johnson-sb
CDFs of johnson-sb
ICDFs of johnson-sb
PDFs of johnson-sl
CDFs of johnson-sl
ICDFs of johnson-sl
PDFs of johnson-su
CDFs of johnson-su
ICDFs of johnson-su
PDFs of kolmogorov-smirnov
CDFs of kolmogorov-smirnov
ICDFs of kolmogorov-smirnov
PDFs of kolmogorov-smirnov+
CDFs of kolmogorov-smirnov+
ICDFs of kolmogorov-smirnov+
PDFs of laplace
CDFs of laplace
ICDFs of laplace
PDFs of levy
CDFs of levy
ICDFs of levy
PDFs of log-logistic
CDFs of log-logistic
ICDFs of log-logistic
PDFs of log-normal
CDFs of log-normal
ICDFs of log-normal
PDFs of logistic
CDFs of logistic
ICDFs of logistic
PDFs of nakagami
CDFs of nakagami
ICDFs of nakagami
PDFs of negative-binomial
CDFs of negative-binomial
ICDFs of negative-binomial
PDFs of normal
CDFs of normal
ICDFs of normal
PDFs of pareto
CDFs of pareto
ICDFs of pareto
PDFs of pascal
CDFs of pascal
ICDFs of pascal
PDFs of pearson-6
CDFs of pearson-6
ICDFs of pearson-6
PDFs of poisson
CDFs of poisson
ICDFs of poisson
PDFs of power
CDFs of power
ICDFs of power
PDFs of rayleigh
CDFs of rayleigh
ICDFs of rayleigh
PDFs of real-discrete-distribution
CDFs of real-discrete-distribution
ICDFs of real-discrete-distribution
PDFs of reciprocal-sqrt
CDFs of reciprocal-sqrt
ICDFs of reciprocal-sqrt
PDFs of t
CDFs of t
ICDFs of t
PDFs of triangular
CDFs of triangular
ICDFs of triangular
PDFs of uniform-int
CDFs of uniform-int
ICDFs of uniform-int
PDFs of uniform-real
CDFs of uniform-real
ICDFs of uniform-real
PDFs of watson-g
CDFs of watson-g
ICDFs of watson-g
PDFs of watson-u
CDFs of watson-u
ICDFs of watson-u
PDFs of weibull
CDFs of weibull
ICDFs of weibull
PDFs of zipf
CDFs of zipf
ICDFs of zipf
2d multidimensional normal (mean=[0,0], covariances=I)
2d multidimensional normal (mean=[0,0], covariances=1 -1] [-1 2)
2d dirichlet (alpha=[2,0.8])
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
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)
Jittered (0.5) Halton plot (500 samples)
Jittered (0.5) Sobol plot (500 samples)
Jittered (0.5) Sphere plot (500 samples)
Jittered (0.5) Gaussian plot (500 samples)
Jittered (0.5) Default plot (500 samples)
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
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
Two
Three
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
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)
Halton plot (500 samples)
Sobol plot (500 samples)
Sphere plot (500 samples)
Gaussian plot (500 samples)
Default plot (500 samples)
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
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
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
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).