Finds the distance between p1
and p2
.
The inputs may be plain objects with x
, y
, and/or z
keys, Vector
s,
or Agent
s with x
, y
, and/or z
data.
const a1 = new Agent();
const a2 = new Agent({ x: 3, y: 4 });
distance(a1, a2); // returns 5 (defaults to x = 0 and y = 0 for a1)
const p1 = { x: 0, y: 2 };
const p2 = { x: 0, y: 4 };
distance(p1, p2); // returns 2
Given a mean and standard deviation, returns a value from a normal/Gaussian distribution.
// returns values mostly between 5 and 15 (but sometimes lower or higher)
gaussian(10, 5);
// no parameters defaults to mean = 0, std. dev. = 1
gaussian(); // mostly values between -1 and 1
Finds the greatest common divisor of a
and b
.
gcd(7, 13); // returns 1
gcd(9, 15); // returns 3
gcd(12, 24); // returns 12
Linearly interpolates between x
and y
. The third parameter t
(usually
a value between 0
and 1
) is the amount by which to interpolate — a value of 0
returns the x
value and 1
returns the y
value.
lerp(5, 10, 0.5); // returns 7.5
lerp(0, 100, 0.1); // returns 10
lerp(22, 79, 1); // returns 79
The first value.
The second value.
The amount by which to interpolate (0 returns x, 1 returns y).
Finds the Manhattan distance between p1
and p2
.
The inputs may be plain objects with x
, y
, and/or z
keys, Vector
s,
or Agent
s with x
, y
, and/or z
data.
const a1 = new Agent();
const a2 = new Agent({ x: 3, y: 4 });
manhattanDistance(a1, a2); // returns 7 (defaults to x = 0 and y = 0 for a1)
const p1 = { x: 3, y: 2 };
const p2 = { x: 0, y: 4 };
manhattanDistance(p1, p2); // returns 5
Return the maximum value from an array of numbers.
max([1, 2, 3]); // returns 3
max([10]); // returns 10
max([]); // returns null for empty arrays
Return the mean value from an array of numbers.
mean([1, 2, 3]); // returns 2
mean([10]); // returns 10
mean([]); // returns null for empty arrays
Return the mean value from an array of numbers.
median([1, 2, 3]); // returns 2
median([10]); // returns 10
median([1, 2, 3, 4]); // returns 2.5 (the mean of the two median values)
median([]); // returns null for empty arrays
Return the minimum value from an array of numbers.
min([1, 2, 3]); // returns 1
min([10]); // returns 10
min([]); // returns null for empty arrays
Return percentile value from an array of numbers. If a percentile falls between discrete values of the array, linearly interpolates between those values (https://en.wikipedia.org/wiki/Percentile#The_linear_interpolation_between_closest_ranks_method)
Array of numbers
Percentile value (between 0 and 1 inclusive)
Return a random integer (or float)
between min
and max
If true, returns a float. If false or empty, returns an int.
Maps a number x, from the given domain aMin --> aMax, onto the given range bMin --> bMax. Ex: remap(5, 0, 10, 0, 100) => 50.
The remapped value.
This is a factory function that returns a function that acts like utils.sample
, except it can sample multiple values as an array. Like utils.sample
, the returned function can also sample by weighted values.
How many values the returned sample function should retrieve, when it is called.
Seed a pseudo-random number generator with a value.
This can be used to produce predictable pseudo-random numbers.
When calling utils.random
, utils.sample
, or other functions
relying on randomness with the same initial seed, the values
generated will always be the same.
Predictable randomness can be turned off by calling seed(null)
, or reset
by calling seed(value)
again with the initial value you used.
Per the Hull–Dobell Theorem, this should iterate pseudo-randomly over the range [0...m) with period = m
Creates an array of shuffled values, using a version of the Fisher-Yates shuffle. (This is lodash's implementation).
The array to shuffle.
Returns the new shuffled array.
Find the standard deviation of an Array of numbers.
Find the sum of an Array of numbers.
Produces a pseudo-random value sampled from the range 0-1 (inclusive).
Shortcut for calling random(0, 1, true);
Generated using TypeDoc
Given a
number
andmin
andmax
values, restrict the number to the range specified.0.0.5