Gathering detailed insights and metrics for random-weighted-choice
Gathering detailed insights and metrics for random-weighted-choice
Gathering detailed insights and metrics for random-weighted-choice
Gathering detailed insights and metrics for random-weighted-choice
random-seed-weighted-chooser
A random weighted item chooser with custom seed option for JavaScript and TypeScript.
@l.degener/weighted-random
weighted random choice
random-choice
Get a weighted random item from an array
ivoire-weighted-choice
Weighted choices for the Ivoire random number generator framework.
Node.js module to make a random choice among weighted elements of table.
npm install random-weighted-choice
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
38 Stars
36 Commits
4 Forks
3 Watchers
4 Branches
1 Contributors
Updated on Jun 05, 2025
Latest Version
0.1.4
Package Id
random-weighted-choice@0.1.4
Unpacked Size
13.14 kB
Size
5.14 kB
File Count
8
NPM Version
6.14.5
Node Version
14.2.0
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
Node.js module to make a random choice among weighted elements of table.
With npm do:
1npm install random-weighted-choice
Although you can add several times the same id
1var rwc = require("random-weighted-choice"); 2var table = [ 3 { weight: 1, id: "item1" }, // Element 1 4 { weight: 1, id: "item2" }, // Element 2 5 { weight: 4, id: "item3" }, // Element with a 4 times likelihood 6 { weight: 2, id: "item1" }, // Element 1, weight added with 2 => 3 7]; 8var choosenItem = rwc(table); 9var choosenUnlikely = rwc(table, 100); // The last shall be first 10var choosenDeterministically = rwc(table, 0);
It is better to not use the same twice, if you want a temperature other than the default one (50).
1var rwc = require("random-weighted-choice"); 2var table = [ 3 { weight: 1, id: "item1" }, // Element 1 4 { weight: 1, id: "item2" }, // Element 2 5 { weight: 4, id: "item3" }, // Element with a 4 times likelihood 6 { weight: 2, id: "item4" }, // Element 4 7 { weight: 2, id: "item5" }, 8]; 9var choosenItem = rwc(table); 10var choosenUnlikely = rwc(table, 100); // The last shall be first 11var choosenDeterministically = rwc(table, 0);
Without temperature (second parameter) or a 50 value, likelihoods are:
1{ item1: 10%, item2: 10%, item3: 40%, item4: 20%, item5: 20% }
With a temperature value of 100:
1{ item1: 30%, item2: 30%, item3: 0%, item4: 20%, item5: 20% }
With a temperature value of 0, modified weights are:
1{ item1: 0, item2: 0, item3: 8, item4: 2, item5: 2 }
Return the id
of the chosen item from table
.
The table
parameter should contain an Array. Each item of that Array must
bean object, with at least weight
and id
property.
Weight values are relative to each other. They are integers.
When the sum of the weight values is null
, null
is returned (can't choose).
When the Array is empty, null
is returned.
More explanations on how it works on Everything2.
No vulnerabilities found.
Reason
no binaries found in the repo
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
Found 0/30 approved changesets -- score normalized to 0
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
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
54 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