Weighted Random

weighted-random

Choose a random value from a fixed list of choices. Treat some values as more likely than others.

Released under the MIT license. Current Github Action build status. Follow @pqtdev

Installation

npm install @pqt/weighted-random
# OR
yarn add @pqt/weighted-random

Usage

TypeScript

import { weightedRandom, Items } from "@pqt/weighted-random";

const items: Items = [
  ["Audi", 3],
  ["BMW", 1],
  ["Ferrari", 7],
  ["Lamborghini", 7],
  ["RAM", 4],
  ["Tesla", 6]
];

weightedRandom(items); // => "Lamborghini" (OR any of the other options with a variability based on weight)

JavaScript

import { weightedRandom } from "@pqt/weighted-random";

weightedRandom([
  ["Audi", 3],
  ["BMW", 1],
  ["Ferrari", 7],
  ["Lamborghini", 7],
  ["RAM", 4],
  ["Tesla", 6]
]); // => "Lamborghini" (OR any of the other options with a variability based on weight)

License

MIT

Learn more about Weighted Random

Choose a random value from a fixed list of choices. Treat some values as more likely than others.

Primary Language
TypeScript
Latest Release
v1.0.0
Start Date
March 30th, 2020 at 4:17 PM
Last Updated
about 2 months ago
NPM Downloads
171
View SourceView Source