Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/plantain-00/weighted-picker
A library to pick a random item from weighted array.
https://github.com/plantain-00/weighted-picker
Last synced: 12 days ago
JSON representation
A library to pick a random item from weighted array.
- Host: GitHub
- URL: https://github.com/plantain-00/weighted-picker
- Owner: plantain-00
- License: mit
- Created: 2017-01-26T11:41:20.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-02T23:54:25.000Z (almost 2 years ago)
- Last Synced: 2024-10-04T22:25:32.778Z (about 1 month ago)
- Language: TypeScript
- Size: 1.48 MB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# weighted-picker
[![Dependency Status](https://david-dm.org/plantain-00/weighted-picker.svg)](https://david-dm.org/plantain-00/weighted-picker)
[![devDependency Status](https://david-dm.org/plantain-00/weighted-picker/dev-status.svg)](https://david-dm.org/plantain-00/weighted-picker#info=devDependencies)
[![Build Status: Windows](https://ci.appveyor.com/api/projects/status/github/plantain-00/weighted-picker?branch=master&svg=true)](https://ci.appveyor.com/project/plantain-00/weighted-picker/branch/master)
![Github CI](https://github.com/plantain-00/weighted-picker/workflows/Github%20CI/badge.svg)
[![npm version](https://badge.fury.io/js/weighted-picker.svg)](https://badge.fury.io/js/weighted-picker)
[![Downloads](https://img.shields.io/npm/dm/weighted-picker.svg)](https://www.npmjs.com/package/weighted-picker)
[![gzip size](https://img.badgesize.io/https://unpkg.com/weighted-picker?compression=gzip)](https://unpkg.com/weighted-picker)
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fplantain-00%2Fweighted-picker%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/weighted-picker)A library to pick a random item from weighted array.
## install
`npm i weighted-picker`
## usage
```ts
// nodejs:
import WeightedPicker from "weighted-picker";// browser(module):
// import WeightedPicker from "weighted-picker";// browser(script tag):
//const items = [
{ name: "a", weight: 1 },
{ name: "b", weight: 2 },
{ name: "c", weight: 3 },
{ name: "d", weight: 4 },
];
const picker = new WeightedPicker(items.length, index => items[index].weight);
let index = picker.pickOne();
index = picker.pickOne();
index = picker.pickOne();
```or
```ts
import WeightedPicker from "weighted-picker";const items = ["a", "b", "c", "d"];
const weights = [1, 2, 3, 4];
const picker = new WeightedPicker(items.length, index => weights[index]);
let index = picker.pickOne();
index = picker.pickOne();
index = picker.pickOne();
```## change logs
```ts
// v2
import WeightedPicker from "weighted-picker/nodejs";
import WeightedPicker from "weighted-picker/browser";// v1
import { WeightedPicker } from "weighted-picker";
```