🎲 Extract one or more random elements from a weighted array (aka loot table or gacha)

Overview

CI status GitHub package.json version Stars created by Leonardo Montini youTube video views

wrand

Extract one or more random elements from a weighted array.

const items = [
  { original: "Bronze", weight: 20 },
  { original: "Silver", weight: 10 },
  { original: "Gold", weight: 3 },
  { original: "Platinum", weight: 1 },
];

const picker = new RandomPicker(items);

const result = picker.pick();

⭐️ If you like this library, don't forget to give it a star! ⭐️

Table of contents

Installation

Node

# If you use npm
npm install wrand

# If you use yarn
yarn add wrand

Deno

If you use Deno, you can just import what you need from deno.land/x

import {
  RandomPicker,
  pick,
  pickMany,
  flatten,
} from "https://deno.land/x/wrand/mod.ts";

Usage

Hint: The test.ts file contains all possible examples. Thi is the best source of usage documentation.

RandomPicker object

You can use the RandomPicker object to pick one or more random elements from a weighted array. The array can be any type, objects are fine.

The best usage here is if you need to pick many items at different moments and you don't want to recreate the object every time.

const items = [
  { original: "Bronze", weight: 20 },
  { original: "Silver", weight: 10 },
  { original: "Gold", weight: 3 },
  { original: "Platinum", weight: 1 },
];

const picker = new RandomPicker(items);

Once the item is created, you can call all exposed methods, in particular pick and pickMany.

const result = picker.pick();

const results = picker.pickMany(10);

Standalone methods

If you only need to pick one item sporadically, you can hide the object creation and use the standalone methods instead.

The syntax is much more concise but keep in mind that the object is created underneath, you just don't have the reference and the code looks shorter.

const items = [
  { original: "Bronze", weight: 20 },
  { original: "Silver", weight: 10 },
  { original: "Gold", weight: 3 },
  { original: "Platinum", weight: 1 },
];

const result = pick(items);

Roadmap

This is a high-level roadmap. You can find more in the open issues.

  • RandomPicker class
    • Multiple picks with option for duplicates
    • Handle duplicates
  • Standalone methods
    • pick
    • pickMany
    • flatten
  • Extra features
    • Add badges to the documentation
    • Allow for custom random function
    • Keep coverage high and not forget edge cases

Contributing

Contributions of any kind are welcome! You can find more info on CONTRIBUTING.md.

Comments
  • feat: added pick many distinct

    feat: added pick many distinct

    Added Feature Pick Many Distinct

    • Has capability to pick many but distinct
    • If amount of items needed is more than the length of array or equal to it returns all items

    closes #2

    opened by AnoopFranc 7
  • Prevent duplicates on a single call of pickMany

    Prevent duplicates on a single call of pickMany

    Add a boolean flag on pickMany (or a new function, pickManyDistinct) to prevent duplicates to be returned in a single call.

    Keep an extra eye on cases where the requested amount is higher than the actual items and no duplicates are wanted.

    enhancement help wanted good first issue 
    opened by Balastrong 2
  • Fix format command

    Fix format command

    As is: npm run format does not work on Windows (and probably a --write is missing too!)

    To be: The command works regardless of the OS and all files are formatted properly.

    bug 
    opened by Balastrong 0
  • Add the possiblity of adding a custom random function

    Add the possiblity of adding a custom random function

    We know Math.random() isn't the best random number generator option for many reasons.

    In our case though, it would be a huge bonus being able to provide a custom random function as the user can already have one and our tests would be way more reliable with a random method that accepts a seed.

    A new parameter can be passed to the constructor and the standalone methods to provide the random implementation.

    enhancement help wanted 
    opened by Balastrong 0
  • Add config to RandomPicker to remove items when picked

    Add config to RandomPicker to remove items when picked

    A new boolean option can be passed in the constructor.

    If enabled, every time an item is picked it is also removed from the items list.

    Testing notes: handle when all items have been removed!

    enhancement help wanted good first issue 
    opened by Balastrong 0
  • Improve tests

    Improve tests

    As of v1.0.1 c6fba99741d5c4d8f4d67bc9d446983e2fbecefa the coverage is already 100% but there are for sure some edge cases that are not covered, for example passing a negative amount to pickMany.

    It can be useful to identify such cases and add more tests to validate/strengthen the code.

    help wanted good first issue hacktoberfest 
    opened by Balastrong 0
Releases(v1.2.0)
  • v1.2.0(Nov 27, 2022)

    What's Changed

    • feat: remove items when picked by @Balastrong in https://github.com/Balastrong/wrand/pull/13
    • fix: prettier path by @Balastrong in https://github.com/Balastrong/wrand/pull/16
    • feat: added pick many distinct by @AnoopFranc in https://github.com/Balastrong/wrand/pull/14
    • chore: split actions by @Balastrong in https://github.com/Balastrong/wrand/pull/18

    New Contributors

    • @AnoopFranc made their first contribution in https://github.com/Balastrong/wrand/pull/14

    Full Changelog: https://github.com/Balastrong/wrand/compare/v1.1.0...v1.2.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Aug 24, 2022)

    What's Changed

    • fix: build and bump version by @Balastrong in https://github.com/Balastrong/wrand/pull/12

    Full Changelog: https://github.com/Balastrong/wrand/compare/v1.0.3...v1.1.0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.3(Aug 22, 2022)

    What's Changed

    • chore: Improved ci & added badges by @Balastrong in https://github.com/Balastrong/wrand/pull/6
    • feat: Add coverage action by @Balastrong in https://github.com/Balastrong/wrand/pull/7
    • feat: Improved error messages with reference to why and what went wrong by @Balastrong in https://github.com/Balastrong/wrand/pull/8
    • feat: Add custom random by @Balastrong in https://github.com/Balastrong/wrand/pull/9
    • chore: Bump to version 1.0.3 by @Balastrong in https://github.com/Balastrong/wrand/pull/10

    New Contributors

    • @Balastrong made their first contribution in https://github.com/Balastrong/wrand/pull/6

    Full Changelog: https://github.com/Balastrong/wrand/compare/v1.0.0...v1.0.3

    Source code(tar.gz)
    Source code(zip)
Owner
Leonardo Montini
Web developer, Open Source enthusiast and cat lover 😺
Leonardo Montini
Create a deep copy of a set of matched elements with the dynamic state of all form elements copied to the cloned elements.

jq-deepest-copy FUNCTION: Create a deep copy of a set of matched elements while preserving the dynamic state of any matched form elements. Example Use

Michael Coughlin 5 Oct 28, 2022
Source code and 3D assets for the Rings (for Loot) NFT project

Rings (for Loot) Rings (for Loot) is the first and largest 3D interpretation of an entire category in Loot. Adventurers, builders, and artists are enc

null 46 Dec 24, 2022
Format an array of data objects as a textual table.

st2 st2 is string-table refactored in TypeScirpt with more enhancements: add type information, then it can be used in modern IDE like vscode to work w

null 6 Sep 10, 2022
An easy-to-use library to make your life easier when working with random numbers or random choices in javascript.

vrandom An easy-to-use library to make your life easier when working with random numbers or random choices in javascript. Table of contents Installati

Valerio Cipolla 1 Aug 16, 2022
Another table select prompt plugin of inquirer.js, with powerful table render and filters.

inquirer-table-select-prompt Table row selection prompt for Inquirer.js 动机 现有的 inquirer.js 没有支持表格行选中的命令行交互的插件. 社区内能查找到的,只有一个二维数组的 checkbox,eduardobouc

锂电 3 Jan 7, 2023
An extension of the Map class with more Array-like features.

BetterMap An extension of the Map class with more Array-like features. Installation There ain't no installation. It's a Deno module. Usage import { Be

Neko Of The Abyss 13 Oct 24, 2022
Download all Moodle files with one click. This is a Chrome extension built to save time and effort from downloading files manually one by one!

Moodle Downloader Extension Moodle downloader extension for Chrome. The extension is tested with both the TUM moodle and the official moodle demo. Not

Zhongpin Wang 8 Nov 15, 2022
aka Scaletor, take screenshots of a piece of a map and scale/compare with other parts of the map

scale-a-tron A quick-and-dirty map that lets you compare one area to another. Draw a shape around a region, zoom in to another place on the map, and c

Stamen Design 24 Nov 7, 2022
Plugin that lets you create diagrams from textual representation (aka 'Diagrams as Code') within Logseq

Logseq - Diagrams as Code Plugin that lets you create diagrams (and other visualizations) from textual representation (aka 'Diagrams as Code') within

Nicolai P. Großer 80 Dec 21, 2022
TypeScript plugin for service-to-service (aka. "functionless") cloud integrations.

Functionless λ< Functionless is a TypeScript plugin that transforms TypeScript code into Service-to-Service (aka. "functionless") integrations, such a

sam 303 Jan 2, 2023
Hacking Instructions for the Nokia 800 Tough (aka Bananaphone with KaiOS)

Nokia 800 Tough Hacking Instructions for Arch Linux host system. I don't care about your messed up Windows, sorry. 1. Android Platform Tools Install t

Cookie Engineer 3 Jul 12, 2022
a tweaked hackchat client. aka hackchat++.

hackchat-client-plus A tweaked hackchat client. aka hackchat++. Most code are from https://github.com/hack-chat/main. Hosted at https://hcer.netlify.a

null 7 Dec 24, 2022
An extension to download all you need in the LGU (aka CUHKSZ) Blackboard

LGU Blackboard Downloader A Chromium (Chrome/Edge compatible) browser extension to download all you need in the LGU (aka CUHKSZ) Blackboard 一个Chrome/E

Zcorn 4 Mar 4, 2023
A table component for your Mantine data-rich applications, supporting asynchronous data loading, column sorting, custom cell data rendering, row context menus, dark theme, and more.

Mantine DataTable A "dark-theme aware" table component for your Mantine UI data-rich applications, featuring asynchronous data loading support, pagina

Ionut-Cristian Florescu 331 Jan 4, 2023
This project is used to extract media from various posting platfroms like Twitter, Reddit, Pixiv, Youtube and many other

Social-Picker-API This project is used to extract media from various posting platfroms like Twitter, Reddit, Pixiv, Youtube and many others. It's writ

Serge 11 Nov 29, 2022
The frontend of a full stack application of a personal wallet made with React, Node and MongoDB that allows you to add inputs, outputs and see all your extract.

The frontend of a full stack application of a personal wallet made with React, Node and MongoDB that allows you to add inputs, outputs and see all your extract.

Bernardo Rodrigues 5 Jun 2, 2022
The backend of a full stack application of a personal wallet made with React, Node and MongoDB that allows you to add inputs, outputs and see all your extract.

My first full stack application with the concept of a personal wallet that allows you to create a personal account to keep track of your entire statement by adding incoming and outgoing transactions, as well as calculating the total balance and being able to edit and delete old transactions.

Bernardo Rodrigues 6 Jun 23, 2022
Converts your IPv4 address to a 4x4 2-bit PNG which you can extract the IP from.

IP-to-PNG Converts your IPv4 address to a 4x4 2-bit PNG which you can extract the IP from. https://www.npmjs.com/package/ip2png Run npm install ip2png

Görkem / Federal 18 Nov 30, 2022
Extract data-like things from a website on the fly.

There-should-be-an-API Oh, I think this website needs an API. Extract data-like things from a website on the fly. Demo The demo API is hosted on a 256

JacobLinCool 3 Mar 26, 2022