UNPKG
just-random
Version:
latest (3.2.0)
3.2.0
3.1.1
3.1.0
3.0.1
3.0.0
2.2.2
2.2.1
2.2.0
2.1.1
2.1.0
2.0.0
1.0.0
return a randomly selected element in an array
github.com/angus-c/just
angus-c/just
just-random
/
index.cjs
15 lines
(12 loc)
•
281 B
JavaScript
View Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module
.
exports
= random;
/* random([1, 2, 3]); // one of [1, 2, 3], at random random([1]); // 1 random(); // throws */
function
random
(
arr
) {
if
(!
Array
.
isArray
(arr)) {
throw
new
Error
(
'expected an array'
); }
return
arr[
Math
.
floor
(
Math
.
random
() * arr.
length
)]; }