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

lotto-draw

v1.0.2

Published

A simple tool used to pick random elements from a mutable collection of weighted participants

Downloads

413

Readme

lotto-draw

A simple tool used to pick random elements from a mutable collection of weighted participants, much like a lottery or raffle draw, written in Typescript.

Install

$ npm install lotto-draw

Example

import createLotto from "lotto-draw";

enum ItemType { Common, Uncommon, Rare, Epic }

const lotto = createLotto<ItemType>()
    .add(ItemType.Common, 40)
    .add(ItemType.Uncommon, 20)
    .add(ItemType.Rare, 5);

// The result of the following call to the 'draw' function is likely to be ItemType.Common (40/65 chance), slightly less likely to be ItemType.Uncommon (20/65 chance), and unlikely to be ItemType.Rare (5/65 chance).
let result = lotto.draw();

// We might want ItemType.Epic to be a possible result with a smaller weighting than ItemType.Rare, we can just add this to the existing lotto.
lotto.add(ItemType.Epic); // If the ticket count is omitted then a single ticket will be added.

// There is now a 1/66 chance that the result of the following call to the 'draw' function is ItemType.Epic.
result = lotto.draw();

// We can reduce the number of tickets held by a participant. The following call would result in the chances of any following calls to 'draw' returning ItemType.Uncommon being 10/66.
lotto.remove(ItemType.Uncommon, 10);

// We may want to remove a participant altogether. For example, we don't want ItemType.Common to be a potential result for any following calls to 'draw'.
lotto.remove(ItemType.Common);

Usage

The createLotto function returns a lotto instance which, by default, contains no drawable participants.

import createLotto from "lotto-draw";

const lotto = createLotto();

// The following will return null as we have no ticket-holding participants.
const picked = lotto.draw();

An array of initial participants and ticket counts can be passed as an optional argument or as a lotto creation option.

import createLotto from "lotto-draw";

enum ItemType { Common, Uncommon, Rare, Epic }

// Creating a new Lotto instance, passing an array of initial participants and ticket counts.
let lotto = createLotto([
    [ItemType.Common, 40],
    [ItemType.Uncommon, 20],
    [ItemType.Rare, 5],
    [ItemType.Epic, 1]
]);

// Creating a new Lotto instance, passing an array of initial participants and ticket counts as a lotto creation option.
lotto = createLotto({
    participants: [
        [ItemType.Common, 40],
        [ItemType.Uncommon, 20],
        [ItemType.Rare, 5],
        [ItemType.Epic, 1]
    ]
});

The draw function will return a single winning participant while the drawMultiple function will take a number of tickets to draw and returns an array of winning participants.

import createLotto from "lotto-draw";

const lotto = createLotto().add("A").add("B").add("C");

lotto.draw(); // result: "B"

lotto.drawMultiple(5); // result: ["B", "C", "A", "B", "A"]

The createLotto function can take an options object as an argument instead of an array of initial participants, the properties of which are shown below.

| Option |Type | Description | | :--------------------|:- |:- | | random |() => number| A function returning a number between 0 (inclusive) and 1. | | participants |[TParticipant, number][]| The array of initial lotto participants with their ticket counts.

Using a custom random function

While lotto-draw will use Math.random by default in picking participants when draw or drawMultiple is called, a custom random function can be provided as part of the createLotto options. This function must return a valid floating-point number between 0 (inclusive) and 1 (exclusive). This can be used to seed the random numbers used by lotto-draw.

Lotto Methods

.add(participant, [tickets])

Adds the given participant with the number of tickets speficied. If the number of tickets is not defined then it will default to one. If the participant has already been added to the lotto then the tickets will be added to their existing ticket count.

.remove(participant, [tickets])

Removes the specified number of tickets for the given participant. If the number of tickets is not defined then all tickets held by that participant will be removed.

.draw([options])

Draws a random ticket from the overall ticket pool and returns the ticket-holding participant, or null if no tickets exist.

| Option |Type | Description | | :--------------------|:- |:- | | redrawable |boolean| Whether the ticket is redrawable. Default: true |

.drawMultiple(tickets, [options])

Draws the specified number of winning tickets and returns an array of the participants that hold the winning tickets.

| Option |Type | Description | | :--------------------|:- |:- | | redrawable |boolean| Whether the tickets are redrawable. Default: true | | unique |boolean| Whether duplicate entries of participants with multiple winning tickets should be removed from the result. Default: false |