Fast random sampling using reservoir sampling
npm install reservoirnpm:
console
npm install reservoir
``
..then require Reservoir:
``javascript
var Reservoir = require('reservoir');
``
$3
For the browser, add the following to your pages:
``html
``
$3
...Or using AMD in the browser:
``javascript
require(["reservoir"], function(Reservoir) {
// ...
});
``
Usage
A reservoir is just an array with one very special function added - pushSome.
$3
To create an empty reservoir that will contain a maximum of 3 randomly-chosen items:
``javascript
var myReservoir = Reservoir(3);
``
Now, using that new reservoir we begin to add items into it:
``javascript
var myData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
myData.forEach(function(e) {
myReservoir.pushSome(e);
});
``
At this point, myReservoir will contain example 3 randomly-chosen values from the array myData:
``javascript
myReservoir => [2, 4, 7] // This can be any random subset of myData
``
As we can see, one important property of the Reservoir is that the order of the input is preserved.
$3
Since, Reservoir.pushSome operates much like the real Array.push we can pass pushSome more than one parameter and each one will be pushed in order. That means we can make the previous example more succinct by using Function.apply:
``javascript
myReservoir.pushSome.apply(myReservoir, myData);
``
Since the object returned from Reservoir() is just an array decorated with a pushSome function, all normal array functions are available. Particularly of value are the iterator functions: forEach, filter, map, every, some, reduce, and reduceRight.
$3
TODO: Advanced usage (ie. see Some).
API
$3
#### Parameters
reservoirSize is the maximum size of the reservoir. This is the number of elements to be randomly chosen from the input provided to it using pushSome.
randomNumberGenerator is an optional random number generating function to use in place of the default Math.random.
#### Returns
An empty Reservoir (an Array with the function pushSome added to it).
$3
#### Parameters
datum` one or more elements to consider for inclusion into the reservoir.