box-tapedRandomize Any

A secure randomization utility library that provides various randomization functions, supporting both browser and Node.js environments. You can find it on npm: randomize-anyarrow-up-right.

Features

  • 🔒 Secure Random: Uses crypto.getRandomValues() for cryptographically secure random number generation

  • 🎯 Multiple Types: Supports randomization of array elements, integers, and floating-point numbers

  • ⚖️ Weight Support: Built-in weighted random algorithms

  • 🌐 Cross-platform: Supports both browser and Node.js environments

  • 📦 Multiple Formats: Provides ESM, CJS, UMD and other module formats

  • 🔧 TypeScript: Complete TypeScript type definitions

Installation

npm install randomize-any

Or using pnpm:

pnpm add randomize-any

Usage

Basic Import

import { randomizeAny, randomizeInteger, randomizeFloat } from 'randomize-any';

API Documentation

randomizeAny(list: any[]): any

Randomly select an element from an array.

Parameters:

  • list - The array to randomly select from

Returns:

  • A random element from the array

Example:

randomizeInteger(min: number, max: number): number

Generate a random integer within the specified range.

Parameters:

  • min - Minimum value (inclusive)

  • max - Maximum value (inclusive)

Returns:

  • A random integer within the range

Example:

randomizeFloat(min: number, max: number): number

Generate a random floating-point number within the specified range (precision: 0.01).

Parameters:

  • min - Minimum value (inclusive)

  • max - Maximum value (inclusive)

Returns:

  • A random floating-point number within the range

Example:

getSecureWeightedRandom(weights: number[]): number

Generate a secure random index based on a weight array.

Parameters:

  • weights - Weight array

Returns:

  • A random index based on weight distribution

Example:

Advanced Usage

Combined Usage

Generate Random Data

Browser Support

This library uses the crypto.getRandomValues() API to provide cryptographically secure random numbers. Supported browsers include:

  • Chrome 11+

  • Firefox 21+

  • Safari 3.1+

  • Edge 12+

  • IE 11+

For unsupported environments, it automatically falls back to Math.random().

Module Formats

This package provides multiple module formats:

  • ESM: dist/index.browser.esm.mjs

  • CommonJS: index.js

  • UMD: dist/index.browser.global.js

  • Legacy versions: Compatible versions with polyfills

Development

Websites Using This Library

Here are some websites and applications that use randomize-any in production:

🎮 Gaming & Entertainment

🎯 Utilities & Tools


Want to showcase your website here? Open an issuearrow-up-right or submit a pull request!

Last updated