diff --git a/README.md b/README.md
index 2bcb93460..f185c714d 100644
--- a/README.md
+++ b/README.md
@@ -260,6 +260,15 @@
+### _Uncategorized_
+
+
+View contents
+
+* [`sampleSize`](#samplesize)
+
+
+
---
## 🔌 Adapter
@@ -4361,6 +4370,35 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents)
+---
+ ## _Uncategorized_
+
+### sampleSize
+
+Gets `n` random elements at unique keys from `array` up to the size of `array`.
+
+Shuffle the array using the [Fisher-Yates algorithm](https://github.com/chalarangelo/30-seconds-of-code#shuffle).
+Use `Array.slice()` to get the first `n` elements.
+Omit the second argument, `n` to get only one element at random from the array.
+
+```js
+const sampleSize = ([...arr], n = 1) => {
+ let m = arr.length;
+ while (m) {
+ const i = Math.floor(Math.random() * m--);
+ [arr[m], arr[i]] = [arr[i], arr[m]];
+ }
+ return arr.slice(0, n);
+};
+```
+
+```js
+sampleSize([1, 2, 3], 2); // [3,1]
+sampleSize([1, 2, 3], 4); // [2,3,1]
+```
+
+ [⬆ back to top](#table-of-contents)
+
## Collaborators
diff --git a/docs/index.html b/docs/index.html
index d10bb750a..6a66cb59d 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -59,7 +59,7 @@
wrapper.appendChild(box);
box.appendChild(el);
});
- }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
Gets n random elements at unique keys from array up to the size of array.
Shuffle the array using the Fisher-Yates algorithm. Use Array.slice() to get the first n elements. Omit the second argument, n to get only one element at random from the array.
const sampleSize = ([...arr], n = 1) => {
+ let m = arr.length;
+ while (m) {
+ const i = Math.floor(Math.random() * m--);
+ [arr[m], arr[i]] = [arr[i], arr[m]];
+ }
+ return arr.slice(0, n);
+};
+
\ No newline at end of file
diff --git a/snippets/sampleSize.md b/snippets/sampleSize.md
index f6187bdea..7b2480cba 100644
--- a/snippets/sampleSize.md
+++ b/snippets/sampleSize.md
@@ -7,17 +7,17 @@ Use `Array.slice()` to get the first `n` elements.
Omit the second argument, `n` to get only one element at random from the array.
```js
-const sampleSize = ([...arr],n=1) => {
+const sampleSize = ([...arr], n = 1) => {
let m = arr.length;
while (m) {
const i = Math.floor(Math.random() * m--);
[arr[m], arr[i]] = [arr[i], arr[m]];
}
- return arr.slice(0,n)
-}
+ return arr.slice(0, n);
+};
```
```js
-sampleSize([1,2,3],2); // [3,1]
-sampleSize([1,2,3],4); // [2,3,1]
+sampleSize([1, 2, 3], 2); // [3,1]
+sampleSize([1, 2, 3], 4); // [2,3,1]
```
diff --git a/tag_database b/tag_database
index db90b4d02..b06cb8280 100644
--- a/tag_database
+++ b/tag_database
@@ -114,6 +114,7 @@ RGBToHex:utility
round:math
runPromisesInSeries:function
sample:array
+sampleSize:uncategorized
scrollToTop:browser
sdbm:utility
select:object