# weighted-random-distribution

> Simple utility module to select weighted random numbers

Latest version **1.2.2** (published 2020-05-05) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2020-05-05 |
| First published | 2020-04-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Chris Natale |
| Maintainers | natale |
| Keywords | weighted, random, number |

## Links

- npm: https://www.npmjs.com/package/weighted-random-distribution
- Repository: https://github.com/cnatale/weighted-random-distribution
- Homepage: https://github.com/cnatale/weighted-random-distribution#readme
- Issues: https://github.com/cnatale/weighted-random-distribution/issues
- npm.io page: https://npm.io/package/weighted-random-distribution

## Dependencies (3)

- [jsonschema](https://npm.io/package/jsonschema.md) ^1.2.6
- [lodash.reduce](https://npm.io/package/lodash.reduce.md) ^4.6.0
- [lodash.findkey](https://npm.io/package/lodash.findkey.md) ^4.6.0

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 1.2.2 (latest) — 2020-05-05
- 1.2.1 — 2020-05-05
- 1.2.0 — 2020-05-01
- 1.1.2 — 2020-05-01
- 1.1.1 — 2020-05-01
- 1.1.0 — 2020-05-01
- 1.0.5 — 2020-05-01
- 1.0.4 — 2020-05-01
- 1.0.3 — 2020-04-30
- 1.0.2 — 2020-04-26
- 1.0.1 — 2020-04-26
- 1.0.0 — 2020-04-26

## README

![Node.js CI](https://github.com/cnatale/weighted-random-distribution/workflows/Node.js%20CI/badge.svg)
[![codecov](https://codecov.io/gh/cnatale/weighted-random-distribution/branch/master/graph/badge.svg)](https://codecov.io/gh/cnatale/weighted-random-distribution)
# Weighted Random Distribution
A simple utility module to select from a set of discrete values based on weighted random distribution.

#### Table of Contents
1. [inPlaceUnsorted()](#inPlaceUnsorted)
2. [normalize()](#normalize)

### inPlaceUnsorted()

Perform an in-place unsorted weighted value selection. (The algorithm is ported from Python code [here](https://www.electricmonk.nl/log/2009/12/23/weighted-random-distribution/)).

##### Parameters

* **weights {object}** - An Object with each key representing one of the discrete values that will be selected, and each value representing a weight.

##### Returns
One of the keys from the weights param, with the probability of a key being returned based on the weight values {string}

##### Example
```javascript
inPlaceUnsorted({ 'a': 7, 'b': 2, 'c': 1 })
// output: 70% chance of 'a', 20% chance of 'b', 10 percent chance of 'c'

inPlaceUnsorted({ 'a': .1, 'b': .05, 'c': .05 })
// output: 50% change of 'a', 25% chance of 'b', 25% chance 'c'
```

Numeric values don't need to sum to any particular number, and can be decimal.

### normalize()

A utility method to provide [min-max normalization](https://en.wikipedia.org/wiki/Feature_scaling#Rescaling_(min-max_normalization)) for input weights. Given a starting range of xMin and xMax, returns valueToNormalize mapped to the same relative position in relation to transformedMin and transformedMax. Defaults to a transformed range between 0 and 1.

##### Parameters

 * **valueToNormalize {number}**
 * **xMin {number}** The floor of the normalization range.
 * **xMax {number}** The ceiling of the normalization range.
 * **transformedMin {number}** Default value is 0.
 * **transformedMax {number}** Default value is 1.

##### Returns
The normalized representation of valueToNormalize {number}.

##### Examples
```javascript
normalize(50, 0, 100);
// output: .5

normalize(50, 0, 100, 0, 8);
// output: 4

normalize(20, 10, 20);
// output: 1

normalize(10, 10, 20, 30, 40);
// output: 30

normalize(20, 10, 20, 30, 40);
// output: 40
```

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