# random-weighted-choice

> Node.js module to make a random choice among weighted elements of table.

Latest version **0.1.4** (published 2020-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install random-weighted-choice
pnpm add random-weighted-choice
yarn add random-weighted-choice
bun add random-weighted-choice
```

## 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.4 |
| Published | 2020-06-05 |
| First published | 2012-12-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 1 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 38 |
| Author | François Parmentier |
| Maintainers | parmentf |
| Keywords | random, weighted |

## Links

- npm: https://www.npmjs.com/package/random-weighted-choice
- Repository: https://github.com/parmentf/random-weighted-choice
- Homepage: http://github.com/parmentf/random-weighted-choice
- Issues: https://github.com/parmentf/random-weighted-choice/issues
- npm.io page: https://npm.io/package/random-weighted-choice

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^2.2.0

## Recent versions

- 0.1.4 (latest) — 2020-06-05
- 0.1.3 — 2015-11-28
- 0.1.2 — 2015-11-28
- 0.1.1 — 2012-12-23
- 0.1.0 — 2012-12-23

## README

# Random Weighted Choice

[![Build Status](https://secure.travis-ci.org/parmentf/random-weighted-choice.png?branch=master)](http://travis-ci.org/parmentf/random-weighted-choice)
[![Code Coverage](https://img.shields.io/codecov/c/github/parmentf/random-weighted-choice.svg)](https://codecov.io/github/parmentf/random-weighted-choice)

Node.js module to make a random choice among weighted elements of table.

## Installation

With [npm](http://npmjs.org) do:

```bash
npm install random-weighted-choice
```

## Examples

Although you can add several times the same id

```js
var rwc = require("random-weighted-choice");
var table = [
  { weight: 1, id: "item1" }, // Element 1
  { weight: 1, id: "item2" }, // Element 2
  { weight: 4, id: "item3" }, // Element with a 4 times likelihood
  { weight: 2, id: "item1" }, // Element 1, weight added with 2 => 3
];
var choosenItem = rwc(table);
var choosenUnlikely = rwc(table, 100); // The last shall be first
var choosenDeterministically = rwc(table, 0);
```

It is better to not use the same twice, if you want a temperature other than
the default one (50).

```js
var rwc = require("random-weighted-choice");
var table = [
  { weight: 1, id: "item1" }, // Element 1
  { weight: 1, id: "item2" }, // Element 2
  { weight: 4, id: "item3" }, // Element with a 4 times likelihood
  { weight: 2, id: "item4" }, // Element 4
  { weight: 2, id: "item5" },
];
var choosenItem = rwc(table);
var choosenUnlikely = rwc(table, 100); // The last shall be first
var choosenDeterministically = rwc(table, 0);
```

Without temperature (second parameter) or a 50 value, likelihoods are:

```txt
{ item1: 10%, item2: 10%, item3: 40%, item4: 20%, item5: 20% }
```

With a temperature value of 100:

```txt
{ item1: 30%, item2: 30%, item3: 0%, item4: 20%, item5: 20% }
```

With a temperature value of 0, modified weights are:

```txt
{ item1: 0, item2: 0, item3: 8, item4: 2, item5: 2 }
```

## Usage

### random-weighted-choice(Array table, Number temperature = 50)

Return the `id` of the chosen item from `table`.

The `table` parameter should contain an Array. Each item of that Array must
bean object, with at least `weight` and `id` property.

Weight values are relative to each other. They are integers.

When the sum of the weight values is `null`, `null` is returned (can't choose).

When the Array is empty, `null` is returned.

More explanations on how it works on [Everything2](http://everything2.com/title/Blackboard+temperature).

## Also

- <https://github.com/Schoonology/weighted>

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