> 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.

# sampleSize es-toolkit

Returns a new array of randomly selected elements with the specified size.

```typescript
const sampled = sampleSize(array, size);
```

## Usage

### `sampleSize(array, size)`

Use `sampleSize` when you want to randomly sample multiple elements from an array. It uses Floyd's algorithm to efficiently generate random samples without duplicates. It's useful for extracting samples in surveys or randomly selecting multiple items in games.

```typescript
import { sampleSize } from 'es-toolkit/array';

// Randomly select 3 from a number array.
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const randomNumbers = sampleSize(numbers, 3);
// Returns: [2, 7, 9] (example, actually random)

// Randomly select 2 from a string array.
const fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
const randomFruits = sampleSize(fruits, 2);
// Returns: ['cherry', 'apple'] (example, actually random)
```

You can sample with various sizes.

```typescript
import { sampleSize } from 'es-toolkit/array';

const items = ['a', 'b', 'c', 'd', 'e'];

// Select 1 element
const single = sampleSize(items, 1);
// Returns: ['c'] (example)

// Select same as entire array size (shuffle effect)
const all = sampleSize(items, 5);
// Returns: ['b', 'd', 'a', 'e', 'c'] (example)

// Select empty array
const none = sampleSize(items, 0);
// Returns: []
```

#### Parameters


arrayreadonly T[]required

The array to sample from.


sizenumberrequired

The number of elements to select.

#### Returns

Returns a new array consisting of randomly selected elements.

```typescript
T[]
```

#### Throws

Throws an error if `size` is greater than the length of the array.


Source: es-toolkit

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