New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

random-weighted-choice

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

random-weighted-choice

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

  • 0.1.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Random Weighted Choice

Build Status Code Coverage

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

Installation

With npm do:

npm install random-weighted-choice

Examples

Although you can add several times the same id

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).

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:

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

With a temperature value of 100:

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

With a temperature value of 0, modified weights are:

{ 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.

Also

Keywords

FAQs

Package last updated on 05 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc