-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsample.js
More file actions
31 lines (28 loc) · 791 Bytes
/
sample.js
File metadata and controls
31 lines (28 loc) · 791 Bytes
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
/*
sample(list, [n])
Produce a random sample from the list. Pass a number to return n random elements from the list. Otherwise a single random item will be returned.
_.sample([1, 2, 3, 4, 5, 6]);
=> 4
_.sample([1, 2, 3, 4, 5, 6], 3);
=> [1, 6, 2]
*/
function sample(arr, n) {
if (n > arr.length) {
return n + " must be less than array length";
}
var result = [];
var random;
if (arguments[1] === undefined) {
random = Math.floor(Math.random() * arr.length);
return arr[random];
} else {
for (var i = 0; i < n; i++) {
random = Math.floor(Math.random() * arr.length);
while (result.indexOf(arr[random]) != -1) {
random = Math.floor(Math.random() * arr.length);
}
result.push(arr[random]);
}
}
return result;
}