UNPKG
random-array-index
Version:
latest (1.1.0)
1.1.0
1.0.0
Get the random index of an array
random-array-index
/
index.js
12 lines
(8 loc)
•
261 B
JavaScript
View Raw
1
2
3
4
5
6
7
8
9
10
11
12
'use strict'
var
isArrayLike =
require
(
'is-array-like'
)
module
.
exports
=
function
randomArrayIndex
(
arr
) {
if
(!
isArrayLike
(arr)) {
throw
TypeError
(
'Expected argument to be an array or array-like'
) }
return
Math
.
floor
(
Math
.
random
() * arr.
length
) }