# @l.degener/weighted-random

> weighted random choice

Latest version **0.1.0** (published 2017-04-09) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @l.degener/weighted-random
pnpm add @l.degener/weighted-random
yarn add @l.degener/weighted-random
bun add @l.degener/weighted-random
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2017-04-09 |
| First published | 2017-04-09 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Lukas Degener |
| Maintainers | l.degener |

## Links

- npm: https://www.npmjs.com/package/@l.degener/weighted-random
- Repository: https://github.com/ldegen/weighted-random
- Homepage: https://github.com/ldegen/weighted-random#readme
- Issues: https://github.com/ldegen/weighted-random/issues
- npm.io page: https://npm.io/package/@l.degener/weighted-random

## Recent versions

- 0.1.0 (latest) — 2017-04-09

## README

# weighted-random

## Install

You know the drill:

```
npm install @ldegen/weighed-random
```

## Usage

It works like this: You have a (finite) distribution, i.e. an array
containing 'weights'. Use this to create a `WeigtedRandom` function
that will return indices into this array.

``` javascript
var WeightedRandom = require('@ldegen/weighted-random');

// don't worry, weights will be normalized so that the sum up to 1.0
var wr = WeightedRandom([0 ,12, 6, 3]);

// choice will be 
// 0  never, 
// 1  with probability 4/7, 
// 2  with probability 2/7 or 
// 3  with probability 1/7 

var choice = wr(); 
```

By default, `WeightedRandom` uses `Math.random()`, but you can use any
parameter-less function that returns float between 0.0 (inclusive) and 1.0
(exclusive). Just pass it as second argument like this:

```
var wr = WeightedRandom([0 ,12, 6, Math.PI/2], myCustomMathRandomReplacement);
```

---
_Source: https://npm.io/package/@l.degener/weighted-random · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
