Fast random sampling using reservoir sampling
For Node.js, use
npm install reservoir
var Reservoir = require'reservoir';
For the browser, add the following to your pages:
...Or using AMD in the browser:
A reservoir is just an array with one very special function added -
To create an empty reservoir that will contain a maximum of 3 randomly-chosen items:
var myReservoir = Reservoir3;
Now, using that new reservoir we begin to add items into it:
var myData = 1 2 3 4 5 6 7 8 9 10;myDataforEachmyReservoirpushSomee;;
At this point,
myReservoir will contain example 3 randomly-chosen values from the array
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.
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
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:
TODO: Advanced usage (ie. see
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
randomNumberGenerator is an optional random number generating function to use in place of the default
An empty Reservoir (an Array with the function
pushSome added to it).
datum one or more elements to consider for inclusion into the reservoir.
The current length of the reservoir.
Copyright (C) 2012 Jon-Carlos Rivera
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.