Gathering detailed insights and metrics for @ajhgwdjnpm/quas-perferendis-velit
Gathering detailed insights and metrics for @ajhgwdjnpm/quas-perferendis-velit
Gathering detailed insights and metrics for @ajhgwdjnpm/quas-perferendis-velit
Gathering detailed insights and metrics for @ajhgwdjnpm/quas-perferendis-velit
npm install @ajhgwdjnpm/quas-perferendis-velit
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Read the official documentation.
A random weighted item chooser with custom seed option for JavaScript.
This package allows you to choose an index from an array of weights (simplest case), or an object from an array of objects that each have a customizable "weight"
property.
It also allows you to specify default weights, and to use your own seed for the pseudorandom number generator (PRNG).
If this project helped you, please consider buying me a coffee or sponsoring me. Your support is much appreciated!
npm i @ajhgwdjnpm/quas-perferendis-velit
Two ways:
You can use an array of weight numbers to randomly choose an index in that array.
1
2// ...
3// Returns an index using the weights to determine chance, or -1 if empty.
4Chooser.chooseWeightedIndex(arrayOfWeights);
5// Optionally, you can use a custom seed. Math.random() is used as the default.
6Chooser.chooseWeightedIndex(arrayOfWeights, seed);
7// You can also specify a default weight to use if your array contains
8// non-numbers (this is a failsafe).
9Chooser.chooseWeightedIndex(arrayOfWeights, seed, defaultWeight);
If all weights are 0, -1 is returned.
You can use an array of objects, each with a weight property and number value, to randomly choose one of those objects.
1import Chooser from "@ajhgwdjnpm/quas-perferendis-velit";
2
3// ...
4// Expects each object to have a "weight" property. Returns null if array is empty.
5Chooser.chooseWeightedObject(arrayOfObjects);
6// Uses custom property key, default weight (if weight property is missing), and custom seed.
7Chooser.chooseWeightedObject(
8 arrayOfObjects,
9 weightPropertyKey,
10 defaultWeight,
11 seed
12);
If all weights are 0, null is returned.
For any non-object where an object is expected, or non-number weight where a number is expected:
1
or the optional default value if provided.All negative weights are treated as positive.
If all you need is an index, you can just use a number[] array of weights, like so:
1import Chooser from "@ajhgwdjnpm/quas-perferendis-velit"; 2 3let arrayOfWeights = [10, 50, 35, 5]; 4 5// Returns the randomly chosen index or -1 if the array is empty. 6// Uses Math.random() as the seed. 7Chooser.chooseWeightedIndex(arrayOfWeights); 8// 10% chance of returning 0 9// 50% chance of returning 1 10// 35% chance of returning 2 11// 5% chance of returning 3
Under the hood, this project uses a pseudorandom number generator (PRNG) that allows the developer to provide their own seed.
So if you want to provide your own seed, you can. Seeds can be any value (strings, numbers, etc):
1// Returns the randomly chosen index or -1 if the array is empty.
2// Will return the same result until the seed changes.
3let seed = "myseed";
4Chooser.chooseWeightedIndex(arrayOfWeights, seed); // 3
5Chooser.chooseWeightedIndex(arrayOfWeights, seed); // 3
6Chooser.chooseWeightedIndex(arrayOfWeights, seed); // Always 3
7seed = 12345;
8Chooser.chooseWeightedIndex(arrayOfWeights, seed); // 1
9Chooser.chooseWeightedIndex(arrayOfWeights, seed); // 1
10Chooser.chooseWeightedIndex(arrayOfWeights, seed); // Always 1
1let iceCreamFlavors = [ 2 { flavor: "chocolate", weight: 3 }, 3 { flavor: "vanilla", weight: 1 }, 4 { flavor: "piña colada", weight: 6 } 5]; 6 7// Returns the randomly chosen object based on their weights. 8// - Looks for a property called "weight"; default 1 if not found 9// - Uses Math.random() as the seed. 10Chooser.chooseWeightedObject(iceCreamFlavors); 11// chocolate = 30% chance 12// vanilla = 10% chance 13// piña colada = 60% chance
You can use any number property as the weight. Just provide the property key as the second argument, like so:
1let restaurantRatings = [ 2 { name: "Chipotle", rating: 4.2 }, 3 { name: "Moe's", rating: 4.9 }, 4 { name: "Dirty Bill's", rating: 1.2 } 5]; 6 7// This example uses restaurant ratings as weights. 8Chooser.chooseWeightedObject(restaurantRatings, "rating"); 9// Chipotle = 40.8% chance 10// Moe's = 47.6% chance 11// Dirty Bill's = 11.7% chance
If the property is not found, the default weight is 1
. You can provide a default if you'd like. The seed defaults to Math.random()
, but you can provide your own seed if you'd like as well:
1let restaurantRatings = [ 2 { name: "Chipotle", rating: 4.2 }, 3 { name: "Moe's", rating: 4.9 }, 4 { name: "Edgy Burrito" } // Unrated restaurant 5]; 6 7// Uses "rating" as weight, a default weight of 2.5, and a custom seed. 8let seed = "Brianna's pick"; 9Chooser.chooseWeightedObject(restaurantRatings, "rating", 2.5, seed); 10// Chipotle = 36.2% chance 11// Moe's = 42.2% chance 12// Edgy Burrito = 21.6% chance (no rating property, so uses 2.5 default)
Below we have a lottery with 100000 items in the bag (weights total 100000).
1let lottery = [ 2 { name: "Impossible", weight: 0 }, 3 { name: "Nearly impossible", weight: 1 }, 4 { name: "Very low chance", weight: 9 }, 5 { name: "Low chance", weight: 90 }, 6 { name: "Medium chance", weight: 900 }, 7 { name: "High chance", weight: 9000 }, 8 { name: "Very high chance", weight: 90000 } 9]; 10 11Chooser.chooseWeightedObject(lottery); 12// "Nearly impossible" has 1/100000 odds of occurring.
🎉 Happy Random Seed Weighted Choosing! 🎉
Type definitions have been included for TypeScript support.
Favicon by Twemoji.
Open source software is awesome and so are you. 😎
Feel free to submit a pull request for bugs or additions, and make sure to update tests as appropriate. If you find a mistake in the docs, send a PR! Even the smallest changes help.
For major changes, open an issue first to discuss what you'd like to change.
If you found this project helpful, let the community know by giving it a star: 👉⭐
See LICENSE.md.
No vulnerabilities found.
No security vulnerabilities found.