npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

random-weighted-choice

v0.1.4

Published

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

Downloads

131,604

Readme

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