Liking cljdoc? Tell your friends :D

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 [1,4]. 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 used as into:

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. Return is normalized to [0,1] range:

  • noise - Perlin Noise (gradient noise, 6 octaves, quintic interpolation)
  • vnoise - Value Noise (as in Processing, 6 octaves, hermite interpolation)
  • simplex - Simpled Noise (6 octaves)

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.

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](http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/random/package-summary.html) 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 [1,4].
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...](http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/)
* `: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](http://extremelearning.com.au/a-simple-method-to-construct-isotropic-quasirandom-blue-noise-point-sequences/). 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 used as into:

* 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. Return is normalized to `[0,1]` range:

* [[noise]] - Perlin Noise (gradient noise, 6 octaves, quintic interpolation)
* [[vnoise]] - Value Noise (as in Processing, 6 octaves, hermite interpolation)
* [[simplex]] - Simpled Noise (6 octaves)

#### 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.
raw docstring

billow-noiseclj

source

brandclj

Random boolean with Mersenne Twister RNG.

Random boolean with Mersenne Twister RNG.
sourceraw docstring

default-normalclj

Default normal distribution (u=0.0, sigma=1.0).

Default normal distribution (u=0.0, sigma=1.0).
sourceraw docstring

default-rngclj

Default RNG - JDK

Default RNG - JDK
sourceraw docstring

discrete-noisecljmacro

(discrete-noise X)
(discrete-noise X Y)

Discrete noise. Parameters:

  • X (long)
  • Y (long, optional)

Returns double value from [0,1] range

Discrete noise. Parameters:

* X (long)
* Y (long, optional)

Returns double value from [0,1] range
sourceraw docstring

distributioncljmultimethod

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).

Distributions should be called using DistributionProto and RNGProto.

The rest parameters goes as follows:

Real distributions

  • :beta - :alpha (default: 2.0) and :beta (default: 5.0)
  • :cauchy - :median (default: 0.0) and :scale (default: 1.0)
  • :chi-squared - :degrees-of-freedom (default: 1.0)
  • :empirical - :bean-count (default: 1000) and :data as a sequence
  • :enumerated-real - :data as a sequence and :probabilities as a optional sequence
  • :exponential - :mean (default: 1.0)
  • :f - :numerator-degrees-of-freedom (default: 1.0) and :denominator-degrees-of-freedom (default: 1.0)
  • :gamma - :shape (default: 2.0) and :scale (default: 2.0)
  • :gumbel - :mu (default: 1.0) and :beta (default: 2.0)
  • :laplace - :mu (default: 1.0) and :beta (default: 2.0)
  • :levy - :mu (default: 0.0) and :c (default: 1.0)
  • :logistic - :mu (default: 1.0) and :s (default: 2.0)
  • :log-normal - :scale (default: 1.0) and :shape (default: 1.0)
  • :nakagami - :mu (default: 1.0) and :omega (default: 1.0)
  • :normal - :mu (default: 0.0) and :sd (default: 1.0)
  • :pareto - :scale (default: 1.0) and :shape (default: 1.0)
  • :t - :degrees-of-freedom (default: 1.0)
  • :triangular - :a (default: -1.0), :c (default: 0.0) and :b (default: 1.0)
  • :uniform-real - :lower (default: 0.0) and :upper (default: 1.0)
  • :weibull - :alpha (default: 2.0) and :beta (default: 1.0)

Integer distributions

  • :binomial - :trials (default: 20) and :p (default: 0.5)
  • :bernoulli - :p (default: 0.5)
  • :enumerated-int - :data and :probabilities as a sequences
  • :geometric - :p (default: 0.5)
  • :hypergeometric - :population-size (default: 100), :number-of-successes (default: 50) and :sample-size (default: 25)
  • :pascal - :r (default: 5) and :p (default: 0.5)
  • :poisson - :p (default: 0.5), :epsilon (default: 1.0e-12), :max-iterations (default: 10000000)
  • :uniform-int - :lower (default: 0) and :upper (default: Integer/MAX_VALUE)
  • :zipf - :number-of-elements (default: 100) and :exponent (default: 3.0)
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`).

Distributions should be called using [[DistributionProto]] and [[RNGProto]].

The rest parameters goes as follows:

#### Real distributions

* `:beta` - `:alpha` (default: 2.0) and `:beta` (default: 5.0)
* `:cauchy` - `:median` (default: 0.0) and `:scale` (default: 1.0)
* `:chi-squared` - `:degrees-of-freedom` (default: 1.0)
* `:empirical` - `:bean-count` (default: 1000) and `:data` as a sequence
* `:enumerated-real` - `:data` as a sequence and `:probabilities` as a optional sequence
* `:exponential` - `:mean` (default: 1.0)
* `:f` - `:numerator-degrees-of-freedom` (default: 1.0) and `:denominator-degrees-of-freedom` (default: 1.0)
* `:gamma` - `:shape` (default: 2.0) and `:scale` (default: 2.0)
* `:gumbel` - `:mu` (default: 1.0) and `:beta` (default: 2.0)
* `:laplace` - `:mu` (default: 1.0) and `:beta` (default: 2.0)
* `:levy` - `:mu` (default: 0.0) and `:c` (default: 1.0)
* `:logistic` - `:mu` (default: 1.0) and `:s` (default: 2.0)
* `:log-normal` - `:scale` (default: 1.0) and `:shape` (default: 1.0)
* `:nakagami` - `:mu` (default: 1.0) and `:omega` (default: 1.0)
* `:normal` - `:mu` (default: 0.0) and `:sd` (default: 1.0)
* `:pareto` - `:scale` (default: 1.0) and `:shape` (default: 1.0)
* `:t` - `:degrees-of-freedom` (default: 1.0)
* `:triangular` - `:a` (default: -1.0), `:c` (default: 0.0) and `:b` (default: 1.0)
* `:uniform-real` - `:lower` (default: 0.0) and `:upper` (default: 1.0)
* `:weibull` - `:alpha` (default: 2.0) and `:beta` (default: 1.0)

#### Integer distributions

* `:binomial` - `:trials` (default: 20) and `:p` (default: 0.5)
* `:bernoulli` - `:p` (default: 0.5) 
* `:enumerated-int` - `:data` and `:probabilities` as a sequences
* `:geometric` - `:p` (default: 0.5)
* `:hypergeometric` - `:population-size` (default: 100), `:number-of-successes` (default: 50) and `:sample-size` (default: 25)
* `:pascal` - `:r` (default: 5) and `:p` (default: 0.5)
* `:poisson` - `:p` (default: 0.5), `:epsilon` (default: 1.0e-12), `:max-iterations` (default: 10000000)
* `:uniform-int` - `:lower` (default: 0) and `:upper` (default: `Integer/MAX_VALUE`)
* `:zipf` - `:number-of-elements` (default: 100) and `:exponent` (default: 3.0)
sourceraw docstring

DistributionProtocljprotocol

Get information from distributions.

Get information from distributions.

cdfclj

(cdf d v)
(cdf d v1 v2)

Cumulative probability.

Cumulative probability.

dimensionsclj

(dimensions d)

Returns dimensions

Returns dimensions

icdfclj

(icdf d p)

Inversed cumulative probability

Inversed cumulative probability

lpdfclj

(lpdf d v)

Log density

Log density

pdfclj

(pdf d v)

Density

Density

probabilityclj

(probability d v)

Probability (PMF)

Probability (PMF)

sampleclj

(sample d)

Returns random sample.

Returns random sample.

source-objectclj

(source-object d)

Returns Java object from backend library

Returns Java object from backend library
sourceraw docstring

distributions-listclj

List of distributions.

List of distributions.
sourceraw docstring

drandclj

(drand)
(drand mx)
(drand mn mx)

Random double number with Mersenne Twister RNG.

Random double number with Mersenne Twister RNG.
sourceraw docstring

fbm-noiseclj

source

flipclj

(flip)
(flip p)

Returns 1 with given probability, 0 otherwise

Returns 1 with given probability, 0 otherwise
sourceraw docstring

frandclj

Random float number with Mersenne Twister RNG.

Random float number with Mersenne Twister RNG.
sourceraw docstring

grandclj

(grand)
(grand stddev)
(grand mean stddev)

Random gaussian double number with Mersenne Twister RNG.

Random gaussian double number with Mersenne Twister RNG.
sourceraw docstring

interpolationsclj

List of possible noise interpolations as a map of names and values.

List of possible noise interpolations as a map of names and values.
sourceraw docstring

irandclj

(irand)
(irand mx)
(irand mn mx)

Random integer number with Mersenne Twister RNG.

Random integer number with Mersenne Twister RNG.
sourceraw docstring

jittered-sequence-generatorclj

(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).

See also sequence-generator.

Create jittered sequence generator.

Suitable for `:r2`, `:sobol` and `:halton` sequences.

`jitter` parameter range is from `0` (no jitter) to `1` (full jitter).

See also [[sequence-generator]].
sourceraw docstring

likelihoodclj

(likelihood d vs)

Likelihood of samples

Likelihood of samples
sourceraw docstring

log-likelihoodclj

(log-likelihood d vs)

Log likelihood of samples

Log likelihood of samples
sourceraw docstring

lrandclj

(lrand)
(lrand mx)
(lrand mn mx)

Random long number with Mersenne Twister RNG.

Random long number with Mersenne Twister RNG.
sourceraw docstring

MultivariateDistributionProtocljprotocol

Get information from distributions.

Get information from distributions.

covarianceclj

(covariance d)

Variance

Variance

meansclj

(means d)

Mean

Mean
sourceraw docstring

noiseclj

(noise x)
(noise x y)
(noise x y z)

Create improved Perlin Noise.

6 octaves, quintic interpolation.

Create improved Perlin Noise.

6 octaves, quintic interpolation.
sourceraw docstring

noise-typesclj

List of possible noise types as a map of names and values.

List of possible noise types as a map of names and values.
sourceraw docstring

observeclj

(observe d v)

Log of probability/density of the value. Alias for [[lpdf]].

Log of probability/density of the value. Alias for [[lpdf]].
sourceraw docstring

random-noise-cfgclj

(random-noise-cfg)

Create random noise configuration.

Create random noise configuration.
sourceraw docstring

random-noise-fnclj

(random-noise-fn)
(random-noise-fn cfg)

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.

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.
sourceraw docstring

randvalcljmacro

(randval v1 v2)
(randval prob v1 v2)

Retrun value with given probability (default 0.5)

Retrun value with given probability (default 0.5)
sourceraw docstring

ridgedmulti-noiseclj

source

rngcljmultimethod

Create RNG for given name (as keyword) and optional seed. Return object enhanced with RNGProto. See: rngs-list for names.

Create RNG for given name (as keyword) and optional seed. Return object enhanced with [[RNGProto]]. See: [[rngs-list]] for names.
sourceraw docstring

RNGProtocljprotocol

Defines set of random functions for different RNGs or distributions returning primitive values.

Defines set of random functions for different RNGs or distributions returning primitive values.

->seqclj

(->seq t)
(->seq t n)

Returns sequence of random samples limited to optional n values.

Returns sequence of random samples limited to optional `n` values.

brandomclj

(brandom t)
(brandom t thr)

Boolean random. Returns true or false with equal probability. You can set probability for true setting thr (from [0-1] range).

See brand.

Boolean random.
Returns true or false with equal probability. You can set probability for `true` setting `thr` (from `[0-1]` range).

See [[brand]].

drandomclj

(drandom t)
(drandom t mx)
(drandom t mn mx)

Random double.

For RNGs: 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).

See drand.

For distributions, just returns random double (call without parameters).

Random double.

For RNGs:
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)`.

See [[drand]].

For distributions, just returns random double (call without parameters).

frandomclj

(frandom t)
(frandom t mx)
(frandom t mn mx)

Random float.

For RNGs: 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).

See frand.

For distributions, just returns random float (call without parameters).

Random float.

For RNGs:
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)`.

See [[frand]].

For distributions, just returns random float (call without parameters).

grandomclj

(grandom t)
(grandom t std)
(grandom t mean std)

Random double from gaussian distribution. 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).

See grand.

Random double from gaussian distribution.
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)`.

See [[grand]].

irandomclj

(irandom t)
(irandom t mx)
(irandom t mn mx)

Random integer.

For RNGs: 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).

See irand.

For distributions, just returns random integer (call without parameters).

Random integer.

For RNGs:
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)`.

See [[irand]].

For distributions, just returns random integer (call without parameters).

lrandomclj

(lrandom t)
(lrandom t mx)
(lrandom t mn mx)

Random long.

For RNGs: As default returns random long from full long range. When mx is passed, range is set to [0, mx). When mn is passed, range is set to [mn, mx).

See lrand.

For distributions, just returns random long (call without parameters).

Random long.

For RNGs:
As default returns random long from full long range. 
When `mx` is passed, range is set to `[0, mx)`. When `mn` is passed, range is set to `[mn, mx)`.

See [[lrand]].

For distributions, just returns random long (call without parameters).

set-seed!clj

(set-seed! t v)

Sets seed. Returns RNG or distribution itself.

Sets seed. Returns RNG or distribution itself.
sourceraw docstring

rngs-listclj

List of all possible RNGs.

List of all possible RNGs.
sourceraw docstring

sequence-generatorcljmultimethod

Create Sequence generator. See sequence-generators-list for names.

Values:

  • :r2, :halton, :sobol, :default - range [0-1] for each dimension
  • :gaussian - from N(0,1) distribution
  • :sphere - from surface of unit sphere (ie. euclidean distance from origin equals 1.0)

Possible dimensions:

  • :r2 - 1-4
  • :halton - 1-40
  • :sobol - 1-1000
  • the rest - 1+

See also jittered-sequence-generator.

Create Sequence generator. See [[sequence-generators-list]] for names.

Values:

* `:r2`, `:halton`, `:sobol`, `:default` - range `[0-1] for each dimension`
* `:gaussian` - from `N(0,1)` distribution
* `:sphere` -  from surface of unit sphere (ie. euclidean distance from origin equals 1.0)

Possible dimensions:

* `:r2` - 1-4
* `:halton` - 1-40
* `:sobol` - 1-1000
* the rest - 1+

See also [[jittered-sequence-generator]].
sourceraw docstring

sequence-generators-listclj

List of random sequence generator. See sequence-generator.

List of random sequence generator. See [[sequence-generator]].
sourceraw docstring

simplexclj

(simplex x)
(simplex x y)
(simplex x y z)

Create Simplex noise. 6 octaves.

Create Simplex noise. 6 octaves.
sourceraw docstring

single-noiseclj

source

synced-rngclj

(synced-rng m)
(synced-rng m seed)

Create synchronized RNG for given name and optional seed. Wraps rng method.

Create synchronized RNG for given name and optional seed. Wraps [[rng]] method.
sourceraw docstring

UnivariateDistributionProtocljprotocol

lower-boundclj

(lower-bound d)

Lower value

Lower value

meanclj

(mean d)

Mean

Mean

upper-boundclj

(upper-bound d)

Higher value

Higher value

varianceclj

(variance d)

Variance

Variance
source

vnoiseclj

(vnoise x)
(vnoise x y)
(vnoise x y z)

Value Noise.

6 octaves, Hermite interpolation (cubic, h01).

Value Noise.

6 octaves, Hermite interpolation (cubic, h01).
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close