Skip to content

random

Generates a random number within a specified range.

Syntax

typescript
random(min?: number, max?: number): number

Parameters

NameTypeDescription
minnumberMinimum value (inclusive). Default: 0.
maxnumberMaximum value (exclusive). Default: 1.

Return

TypeDescription
numberA random number in the range [min, max).

Example

typescript
random(); // Example: 0.472
random(1, 10); // Example: 7.384
random(-5, 5); // Example: -2.17

Notes

  • Throws TypeError if min or max are not numbers.
  • If only min is provided, generates a number between 0 (inclusive) and min (exclusive).
  • If min is greater than or equal to max, always returns min.

References

Released under the MIT License.