Options
All
• Public
• Public/Protected
• All

# Namespace utils

uuid: Function

## Functions

### clamp

• clamp(x: number, min: number, max: number): number
• Given a `number` and `min` and `max` values, restrict the number to the range specified.

``````clamp(5, 1, 10); // returns 5
clamp(5, 2, 4); // returns 4
clamp(0, -4, -3); // returns -3
``````
since

0.0.5

### distance

• distance(p1: Point | Agent, p2: Point | Agent): number
• 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
``````
since

0.0.10

### gaussian

• gaussian(mean?: number, sd?: number): number
• 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
``````
since

0.0.8

### gcd

• gcd(a: number, b: number): number
• Finds the greatest common divisor of `a` and `b`.

``````gcd(7, 13); // returns 1
gcd(9, 15); // returns 3
gcd(12, 24); // returns 12
``````
since

0.4.5

### lerp

• lerp(x: number, y: number, t: number): number
• 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
``````
since

0.2.4

#### Parameters

• ##### x: number

The first value.

• ##### y: number

The second value.

• ##### t: number

The amount by which to interpolate (0 returns x, 1 returns y).

### manhattanDistance

• manhattanDistance(p1: Point | Agent, p2: Point | Agent): number
• 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
``````
since

0.0.12

### max

• max(arr: number[]): number
• 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
``````
since

0.2.0

### mean

• mean(arr: number[]): number
• 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
``````
since

0.0.16

### median

• median(arr: number[]): number
• 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
``````
since

0.2.0

### min

• min(arr: number[]): number
• 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
``````
since

0.2.0

### percentile

• percentile(arr: number[], n: number): number
• 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)

since

0.3.14

#### Parameters

• ##### arr: number[]

Array of numbers

• ##### n: number

Percentile value (between 0 and 1 inclusive)

### random

• random(min?: number, max?: number, float?: boolean): number
• Return a random integer (or float) between `min` and `max`

since

0.1.4

#### Parameters

• ##### float: boolean = false

If true, returns a float. If false or empty, returns an int.

### remap

• remap(x: number, aMin: number, aMax: number, bMin: number, bMax: number): number
• Maps a number x, from the given domain aMin --> aMax, onto the given range bMin --> bMax. Ex: remap(5, 0, 10, 0, 100) => 50.

since

0.0.5

#### Returns number

The remapped value.

### Let sample

• sample<T>(array: T[], weights?: number[]): T

### sampler

• sampler(n: number): SampleFunc | MultipleSampleFunc
• 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.

since

0.5.16

#### Parameters

• ##### n: number

How many values the returned sample function should retrieve, when it is called.

### Const seed

• seed(value: any): void
• 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.

since

0.5.0

### series

• series(m: number): IterableIterator<number>
• Per the Hullâ€“Dobell Theorem, this should iterate pseudo-randomly over the range [0...m) with period = m

since

0.4.8

### shuffle

• shuffle<T>(array: T[]): T[]
• Creates an array of shuffled values, using a version of the Fisher-Yates shuffle. (This is lodash's implementation).

since

0.0.7

#### Parameters

• ##### array: T[]

The array to shuffle.

#### Returns T[]

Returns the new shuffled array.

### stdDev

• stdDev(arr: number[]): number
• Find the standard deviation of an Array of numbers.

since

0.0.16

### sum

• sum(arr: number[]): number
• Find the sum of an Array of numbers.

since

0.0.16

### uniform

• uniform(): number
• Produces a pseudo-random value sampled from the range 0-1 (inclusive). Shortcut for calling `random(0, 1, true);`

since

0.5.0

### zfill

• zfill(str: string, width?: number): string
• since

0.1.4

#### Returns string

Generated using TypeDoc