Gathering detailed insights and metrics for random-seed-weighted-chooser
Gathering detailed insights and metrics for random-seed-weighted-chooser
Gathering detailed insights and metrics for random-seed-weighted-chooser
Gathering detailed insights and metrics for random-seed-weighted-chooser
🎲 A random weighted item chooser with custom seed option for JS.
npm install random-seed-weighted-chooser
Typescript
Module System
Node Version
NPM Version
TypeScript (92.83%)
MDX (5.28%)
HTML (1.9%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
9 Stars
71 Commits
3 Forks
2 Watchers
2 Branches
1 Contributors
Updated on Feb 18, 2025
Latest Version
1.1.11
Package Id
random-seed-weighted-chooser@1.1.11
Unpacked Size
27.06 kB
Size
7.29 kB
File Count
8
NPM Version
10.8.2
Node Version
20.18.0
Published on
Feb 18, 2025
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
1
31
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 save you time, please consider buying me a coffee, which powers my development (and life). Your support is much appreciated!
npm i random-seed-weighted-chooser
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 "random-seed-weighted-chooser";
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 "random-seed-weighted-chooser"; 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: 👉⭐
Want to support the project? Feel free to grab me a coffee, which is my main source of fuel for development:
See LICENSE.md.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
28 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More