> For AI agents: the complete documentation index is available at https://massaman.dev/llms.txt, the full documentation bundle is available at https://massaman.dev/llms-full.txt.

# random es-toolkit

Generates a random number within a specified range. The number can include decimals.

```typescript
const randomNumber = random(min, max);
```

## Usage

### `random(maximum)` / `random(minimum, maximum)`

Use `random` when you need a random number. It generates numbers with decimal points.

```typescript
import { random } from 'es-toolkit/math';

// Generate a random decimal between 0 and 5 (exclusive).
const num1 = random(5);
console.log(num1); // e.g., 2.718281828

// Generate a random decimal between 2 and 10 (exclusive).
const num2 = random(2, 10);
console.log(num2); // e.g., 7.158765432

// Can be used with negative ranges.
const num3 = random(-5, -1);
console.log(num3); // e.g., -3.842134567

// Decimal ranges are also possible.
const num4 = random(1.5, 2.5);
console.log(num4); // e.g., 1.923456789
```

Throws an error if the range is invalid.

```typescript
import { random } from 'es-toolkit/math';

// An error occurs if the maximum value is 0 or less.
try {
  random(0);
} catch (error) {
  console.error(error.message); // 'Invalid input: The maximum value must be greater than the minimum value.'
}

// An error occurs if the minimum value is greater than or equal to the maximum value.
try {
  random(5, 3);
} catch (error) {
  console.error(error.message); // 'Invalid input: The maximum value must be greater than the minimum value.'
}
```

#### Parameters


maximumnumberrequired

The maximum value (exclusive) when using a single parameter. Must be greater than 0.


minimumnumberrequired

The minimum value (inclusive).


maximumnumberrequired

The maximum value (exclusive). Must be greater than the minimum value.

#### Returns

Returns a random decimal within the specified range.

```typescript
number
```

#### Throws

Throws an error if the maximum value is less than or equal to the minimum value.


Source: es-toolkit

Re-exported verbatim from [`es-toolkit`](https://es-toolkit.dev/reference/math/random.html). Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.
