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

@masx200/weighted-randomly-select

v2.1.1

Published

Randomly select from a list of weighted possible outcomes.

Downloads

1,229

Readme

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.

thanks to author Rifdhan.

https://github.com/Rifdhan/weighted-randomly-select

add typescript support

https://github.com/masx200/weighted-randomly-select/blob/master/index.d.ts

chance can be provided as Infinity

assert.equal(
    9,
    Randomly.select([
        {
            chance: Infinity,
            result: 9,
        },
        {
            chance: 6,
            result: 19,
        },
    ])
);

使用了二分搜索加快查找速度

Installation

NPM:

npm install --save @masx200/weighted-randomly-select

YarnPkg:

yarn add @masx200/weighted-randomly-select

deno

import {} from "https://deno.land/x/masx200_weighted_randomly_select/mod.ts";

Examples

let Randomly = require("@masx200/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.