secure-weighted-sample

[![Build Status](https://travis-ci.org/emilbayes/secure-weighted-sample.svg?branch=master)](https://travis-ci.org/emilbayes/secure-weighted-sample)

Last updated 6 months ago by
emilbayes .

ISC ·
Repository ·
Bugs ·
Original npm ·
Tarball ·
package.json
$ cnpm install secure-weighted-sample

SYNC missed versions from
official npm registry.

`secure-weighted-sample`

Weighted sampling using a secure RNG in O(n)

```
// Either approx sampling with percentage weights (imprecision stems from the passed weights)
var sample = require('secure-weighted-sample')
console.log(sample([0.1, 0.5, 0.2, 0.05, 0.15]))
// or exact sampling using integers
var sampleInteger = require('secure-weighted-sample/integer')
console.log(sampleInteger([8, 40, 16, 4, 6]))
```

`const idx = sample(weights)`

Approximate sampling using decimal percentage weights. Imprecision occurs if the passed weights are not exactly representable in the double floating point interval `[0, 1)`

. Only performs a single sample from the RNG and does a linear pass over the weights until the random number is less than the cumulative weights. Hence `O(n)`

.

Returns the index from the weights array, which can be used to index another data array or used directly.

`const idx = sampleInteger(integerWeights)`

Exact sampling using integer weights. The sum of weights must be less than `Number.MAX_SAFE_INTEGER`

. The RNG is sampled until a suitable candidate is found. The expected number of samples is 2. If the sum of weights is a power of 2, only a single sample is needed. It then does a linear pass over the weights until the random number is less than the cumulative weights. Hence `O(n)`

.

Returns the index from the weights array, which can be used to index another data array or used directly.

```
npm install secure-weighted-sample
```

- 1.0.0 ... latest (6 months ago)

- 1.0.0 ... 6 months ago

Maintainers (1)

Downloads

Today
0

This Week
0

This Month
0

Last Day
0

Last Week
0

Last Month
1

Dependencies (3)

- nanoassert ^2.0.0
- secure-random-double ^1.0.2
- secure-random-uniform ^4.0.0

Dev Dependencies (1)

- standard ^14.3.4

Dependents (0)

None
Copyright 2014 - 2016 © taobao.org |