forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.ts
More file actions
36 lines (34 loc) · 1.04 KB
/
Copy pathsample.ts
File metadata and controls
36 lines (34 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* Produces a random number between the inclusive `lower` and `upper` bounds.
*/
function randomInteger(lower: number, upper: number): number {
return lower + Math.floor(Math.random() * (upper - lower + 1));
}
/**
* Returns a random element from the given array.
*
* @typeParam T The type of the elements in the array.
* @typeParam O The type of the accumulator.
*
* @param array The array to sample from.
*
* @returns A random element from the given array, or `undefined` if the array
* is empty.
*
* @example Basic usage
* ```ts
* import { sample } from "@std/collections/sample";
* import { assertArrayIncludes } from "@std/assert";
*
* const numbers = [1, 2, 3, 4];
* const random = sample(numbers);
*
* assertArrayIncludes(numbers, [random]);
* ```
*/
export function sample<T>(array: readonly T[]): T | undefined {
const length = array.length;
return length ? array[randomInteger(0, length - 1)] : undefined;
}