forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampleSize.js
40 lines (38 loc) · 990 Bytes
/
sampleSize.js
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
37
38
39
40
import copyArray from './.internal/copyArray.js'
import slice from './slice.js'
/**
* Gets `n` random elements at unique keys from `array` up to the
* size of `array`.
*
* @since 4.0.0
* @category Array
* @param {Array} array The array to sample.
* @param {number} [n=1] The number of elements to sample.
* @returns {Array} Returns the random elements.
* @example
*
* sampleSize([1, 2, 3], 2)
* // => [3, 1]
*
* sampleSize([1, 2, 3], 4)
* // => [2, 3, 1]
*/
function sampleSize(array, n) {
n = n == null ? 1 : n
const length = array == null ? 0 : array.length
if (!length || n < 1) {
return []
}
n = n > length ? length : n
let index = -1
const lastIndex = length - 1
const result = copyArray(array)
while (++index < n) {
const rand = index + Math.floor(Math.random() * (lastIndex - index + 1))
const value = result[rand]
result[rand] = result[index]
result[index] = value
}
return slice(result, 0, n)
}
export default sampleSize