Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

weighted-randomly-select

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weighted-randomly-select

Randomly select from a list of weighted possible outcomes.

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by500%
Maintainers
1
Weekly downloads
 
Created
Source

Weighted-Randomly-Select

This package lets you easily perform weighted random selection. Provide an array of objects with chance and result properties, and a random result will be selected according to the provided chance values.

Installation

NPM: npm install --save weighted-randomly-select

YarnPkg: yarn add weighted-randomly-select

Examples

let Randomly = require("weighted-randomly-select");

// Every outcome can be equiprobable
let name = Randomly.select([{
  chance: 1, result: "John"
}, {
  chance: 1, result: "Mary"
}]);
// name will be either "John" or "Mary"

// Some outcomes can be more probable than others
let coin = Randomly.select([{
  chance: 1, result: "Heads"
}, {
  chance: 1, result: "Tails"
}, {
  chance: 0.01, result: "Side"
}]);
// coin will be either "Heads", "Tails", or "Side"

// Chance values can be any positive number, and result values
// can be anything other than null and undefined
let item = Randomly.select([{
  chance: 0.8, result: { someField: "someValue" }
}, {
  chance: 20, result: 42
}, {
  chance: 1.5, result: [3, 1, 5]
}]);
// item will be either { someField: "someValue" }, 42, or [3, 1, 5]

API Reference

Randomly.select(options)

options: an array of objects, each of which having a chance number value and result value

chance: any positive floating point value (zero is permitted but such an entry will never be selected)

Note: all chance values must add up to a value greater than zero (i.e. there should be something to select). These values do not have to add up to 1 or any other specific value.

Return value: one of the result values in the options array.

Throws errors if any input is invalid.

Randomly.selectWithoutValidation(options)

Same as above but does not perform any validation on the provided input. If you're encountering unexpected issues, try using the above method, which does the same thing but with input validation.

FAQs

Package last updated on 25 Jul 2017

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