
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
randomize-any
Advanced tools
A secure randomization utility library that provides various randomization functions, supporting both browser and Node.js environments.
A secure randomization utility library that provides various randomization functions, supporting both browser and Node.js environments.
crypto.getRandomValues() for cryptographically secure random number generationnpm install randomize-any
Or using pnpm:
pnpm add randomize-any
import { randomizeAny, randomizeInteger, randomizeFloat } from 'randomize-any';
randomizeAny(list: any[]): anyRandomly select an element from an array.
Parameters:
list - The array to randomly select fromReturns:
Example:
const fruits = ['apple', 'banana', 'orange', 'grape'];
const randomFruit = randomizeAny(fruits);
console.log(randomFruit); // e.g.: 'banana'
const numbers = [1, 2, 3, 4, 5];
const randomNumber = randomizeAny(numbers);
console.log(randomNumber); // e.g.: 3
randomizeInteger(min: number, max: number): numberGenerate a random integer within the specified range.
Parameters:
min - Minimum value (inclusive)max - Maximum value (inclusive)Returns:
Example:
const randomInt = randomizeInteger(1, 10);
console.log(randomInt); // e.g.: 7
const diceRoll = randomizeInteger(1, 6);
console.log(diceRoll); // e.g.: 4
randomizeFloat(min: number, max: number): numberGenerate a random floating-point number within the specified range (precision: 0.01).
Parameters:
min - Minimum value (inclusive)max - Maximum value (inclusive)Returns:
Example:
const randomFloat = randomizeFloat(0, 1);
console.log(randomFloat); // e.g.: 0.73
const temperature = randomizeFloat(20.0, 30.0);
console.log(temperature); // e.g.: 25.67
getSecureWeightedRandom(weights: number[]): numberGenerate a secure random index based on a weight array.
Parameters:
weights - Weight arrayReturns:
Example:
// Weights [1, 2, 3], index 2 has the highest probability of being selected
const weights = [1, 2, 3];
const randomIndex = getSecureWeightedRandom(weights);
console.log(randomIndex); // 0, 1, or 2
// Practical application: Select prizes based on weights
const prizes = ['Bronze', 'Silver', 'Gold'];
const prizeWeights = [5, 3, 1]; // Bronze has the highest probability
const selectedPrizeIndex = getSecureWeightedRandom(prizeWeights);
const selectedPrize = prizes[selectedPrizeIndex];
console.log(selectedPrize);
import { randomizeAny, randomizeInteger, getSecureWeightedRandom } from 'randomize-any';
// Create a weighted random selector
function weightedRandomSelect(items, weights) {
const index = getSecureWeightedRandom(weights);
return items[index];
}
const items = ['Common Item', 'Rare Item', 'Legendary Item'];
const weights = [70, 25, 5]; // 70% common, 25% rare, 5% legendary
const selectedItem = weightedRandomSelect(items, weights);
console.log(selectedItem);
// Generate random user data
function generateRandomUser() {
const names = ['John', 'Jane', 'Bob', 'Alice'];
const ages = randomizeInteger(18, 65);
const scores = randomizeFloat(60, 100);
return {
name: randomizeAny(names),
age: ages,
score: Math.round(scores * 100) / 100
};
}
const user = generateRandomUser();
console.log(user);
// e.g.: { name: 'Jane', age: 28, score: 87.34 }
This library uses the crypto.getRandomValues() API to provide cryptographically secure random numbers. Supported browsers include:
For unsupported environments, it automatically falls back to Math.random().
This package provides multiple module formats:
dist/index.browser.esm.mjsindex.jsdist/index.browser.global.js# Install dependencies
pnpm install
# Run tests
pnpm test
# Build
pnpm build
# Generate coverage report
pnpm coverage
Here are some websites and applications that use randomize-any in production:
Want to showcase your website here? Open an issue or submit a pull request!
MIT License
FAQs
A secure randomization utility library that provides various randomization functions, supporting both browser and Node.js environments.
We found that randomize-any demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.