Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@rahulv.dev/weighted-list
Advanced tools
A weighted list provider to pick a random entry on the basis of the weights of the objects.
@rahulv.dev/weighted-list
is a TypeScript library that provides a weighted list implementation. This allows you to add items with associated weights and then randomly select items based on those weights. The library is ideal for scenarios where you need to randomly pick elements with different probabilities.
This package depends on @rahulv.dev/rng
for secure and cryptographic random number generation.
Install both the weighted list and the RNG package:
npm install @rahulv.dev/weighted-list @rahulv.dev/rng
First, import the WeightedList
, WeightedListItem
, and RNG
classes into your TypeScript or JavaScript project:
import { WeightedList } from "@rahulv.dev/weighted-list";
import { RNG } from "@rahulv.dev/rng";
Initialize the RNG before using it in the WeightedList
:
const rng = RNG.init();
const weightedList = new WeightedList<string>(rng);
You can add items with specific weights and pick them randomly:
weightedList.add("apple", 10);
weightedList.add("banana", 20);
const randomItem = await weightedList.pickRandom();
console.log(randomItem); // Likely 'banana'
You can remove items either by their object or by their WeightedListItem
:
const removedItem = weightedList.remove("apple");
const item = new WeightedListItem("banana", 20);
const removedEntry = weightedList.removeItems(item);
Clear all items from the list:
weightedList.clear();
Method | Description |
---|---|
constructor(rng: IRNG) | Creates a new WeightedList instance with a random number generator. |
add(item: T, weight: number): WeightedList<T> | Adds an item with a specified weight to the list. |
pickRandom(): Promise<T> | Asynchronously picks a random item based on the weights. |
pickRandomEntry(): Promise<WeightedListItem<T>> | Asynchronously picks a random entry (item and weight). |
remove(item: T): WeightedListItem<T> | null | Removes an item by its object value and returns the removed entry. |
removeItems(item: WeightedListItem<T>): WeightedListItem<T> | null | Removes an item by its WeightedListItem instance. |
clear(): void | Clears all items from the list. |
Property | Description |
---|---|
obj: T | The object stored in the list item. |
weight: number | The weight associated with the object. |
@rahulv.dev/rng
This package requires @rahulv.dev/rng
, a secure random number generator that uses the native Crypto API for generating cryptographic random numbers. You can generate random numbers within specific ranges or for multiple values at once.
Refer to the @rahulv.dev/rng documentation for more details.
This project is licensed under the MIT License. See the LICENSE file for details.
If you encounter any issues, feel free to open a ticket on the issues page.
Rahul Vashishtha – https://rahulv.dev
FAQs
A weighted list provider to pick a random entry on the basis of the weights of the objects.
The npm package @rahulv.dev/weighted-list receives a total of 0 weekly downloads. As such, @rahulv.dev/weighted-list popularity was classified as not popular.
We found that @rahulv.dev/weighted-list demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.