random
Generates a random number within a specified range.
Syntax
typescript
random(min?: number, max?: number): numberParameters
| Name | Type | Description |
|---|---|---|
min | number | Minimum value (inclusive). Default: 0. |
max | number | Maximum value (exclusive). Default: 1. |
Return
| Type | Description |
|---|---|
number | A random number in the range [min, max). |
Example
typescript
random(); // Example: 0.472
random(1, 10); // Example: 7.384
random(-5, 5); // Example: -2.17Notes
- Throws
TypeErrorifminormaxare not numbers. - If only
minis provided, generates a number between 0 (inclusive) andmin(exclusive). - If
minis greater than or equal tomax, always returnsmin.
