randox
v2.0.0
Published
Random Number, Letter, Alphanumeric, and UUID generator. Lightweight and easy to use.
Maintainers
Readme
Randox
A lightweight and powerful JavaScript library for generating random values - numbers, letters, strings, UUIDs, colors, dates, passwords, and more.
Features
- 🎲 Random numbers with customizable length and ranges
- 🔤 Random letters (lowercase or uppercase)
- 🔢 Alphanumeric strings
- 🆔 UUID v4 generation
- 🎯 Random floats with precision control
- 🎨 Random colors (hex, rgb, hsl)
- 📅 Random dates within ranges
- 🔐 Secure random IDs and passwords
- ✨ Random choices from arrays
- 🌐 Works everywhere: Node.js, browsers, and CDN
- 🪶 Zero dependencies
- ⚡ Simple and intuitive API
- 📦 CommonJS and ES modules support
Installation
npm install randoxUsage
Node.js (CommonJS)
const {
number,
letter,
alphanum,
v4: uuid,
range,
float,
hex,
bool,
choice,
string,
password,
date,
color,
id,
secure
} = require('randox');
console.log(number(6)); // '482916'
console.log(letter(5, true)); // 'HBXWN'
console.log(uuid()); // '550e8400-e29b-41d4-a716-446655440000'
console.log(password(12)); // 'X9k#mP2$nQ5!'Node.js (ES Modules)
import {
number,
letter,
alphanum,
v4 as uuid,
range,
float,
hex,
bool,
choice,
string,
password,
date,
color,
id,
secure
} from 'randox';
console.log(range(1, 100)); // 42
console.log(float(0, 1, 2)); // 0.73
console.log(color('hex')); // '#a3c2f0'Browser (CDN)
<script src="https://unpkg.com/randox/dist/randox.umd.js"></script>
<script>
console.log(Randox.number(4)); // '8261'
console.log(Randox.letter(5)); // 'mxkpt'
console.log(Randox.alphanum(8)); // 'a4x9m2p7'
console.log(Randox.v4()); // 'c9bf9e57-1685-4c89-bafb-ff5af830be8a'
console.log(Randox.range(1, 10)); // 7
console.log(Randox.float(0, 100, 1)); // 45.3
console.log(Randox.hex(6)); // 'a3f8c2'
console.log(Randox.bool()); // true
console.log(Randox.choice(['a', 'b', 'c'])); // 'b'
console.log(Randox.string(10)); // 'aBcDeFgHiJ'
console.log(Randox.password(16)); // 'X9k#mP2$nQ5!wR7@'
console.log(Randox.date('2024-01-01', '2024-12-31')); // Date object
console.log(Randox.color('rgb')); // 'rgb(123, 45, 200)'
console.log(Randox.id('user')); // 'user_a4x9m2p7'
console.log(Randox.secure(32)); // 'a3f8c2...' (64 char hex)
</script>API Reference
number(length)
Generates a random number string with specified length.
Parameters:
length(optional): Number of digits. Default:1
Returns: String of random digits
number(); // '5'
number(3); // '472'
number(10); // '8392847561'letter(length, upper)
Generates random letters.
Parameters:
length(optional): Number of letters. Default:1upper(optional): Generate uppercase iftrue. Default:false
Returns: String of random letters
letter(); // 'p'
letter(5); // 'mxkpt'
letter(5, true); // 'WQRNZ'alphanum(length)
Generates random alphanumeric string.
Parameters:
length(optional): Number of characters. Default:1
Returns: String of random alphanumeric characters
alphanum(); // 'k'
alphanum(8); // 'a4x9m2p7'
alphanum(16); // 'f8k3m9x2p7a1n5q4'v4()
Generates a standard UUID v4.
Returns: UUID v4 string
v4(); // '550e8400-e29b-41d4-a716-446655440000'
v4(); // 'c9bf9e57-1685-4c89-bafb-ff5af830be8a'range(min, max)
Generates a random integer within a range (inclusive).
Parameters:
min: Minimum valuemax: Maximum value
Returns: Random integer between min and max
range(1, 10); // 7
range(0, 100); // 42
range(-50, 50); // -23float(min, max, precision)
Generates a random floating-point number.
Parameters:
min: Minimum valuemax: Maximum valueprecision(optional): Number of decimal places. Default:2
Returns: Random float between min and max
float(0, 1); // 0.73
float(0, 100, 1); // 45.3
float(0, 1, 4); // 0.7284hex(length)
Generates a random hexadecimal string.
Parameters:
length(optional): Number of hex characters. Default:1
Returns: Random hex string
hex(); // 'a'
hex(6); // 'a3f8c2'
hex(8); // '1f4d9b2e'bool()
Generates a random boolean value.
Returns: true or false
bool(); // true
bool(); // falsechoice(array)
Selects a random element from an array.
Parameters:
array: Array to choose from
Returns: Random element from the array
choice(['apple', 'banana', 'orange']); // 'banana'
choice([1, 2, 3, 4, 5]); // 3
choice(['red', 'green', 'blue']); // 'green'string(length, options)
Generates a random string with mixed characters.
Parameters:
length(optional): String length. Default:10options(optional): Character set optionslowercase: Include lowercase letters. Default:trueuppercase: Include uppercase letters. Default:truenumbers: Include numbers. Default:truesymbols: Include symbols. Default:false
Returns: Random string
string(10); // 'aBc3DeF7gH'
string(8, { uppercase: false }); // 'abc123de'
string(12, { symbols: true }); // 'aB3#dE7$fG9!'password(length, options)
Generates a strong random password.
Parameters:
length(optional): Password length. Default:16options(optional): Character set optionslowercase: Include lowercase. Default:trueuppercase: Include uppercase. Default:truenumbers: Include numbers. Default:truesymbols: Include symbols. Default:true
Returns: Secure random password
password(); // 'X9k#mP2$nQ5!wR7@'
password(12); // 'aB3#dE7$fG9!'
password(20, { symbols: false }); // 'aBcDeF123GhIjK456'date(start, end)
Generates a random date within a range.
Parameters:
start(optional): Start date (Date object or ISO string). Default:'1970-01-01'end(optional): End date (Date object or ISO string). Default: current date
Returns: Random Date object
date(); // Random date between 1970 and now
date('2024-01-01', '2024-12-31'); // Random date in 2024
date(new Date('2023-01-01'), new Date()); // Random date since 2023color(format)
Generates a random color.
Parameters:
format(optional): Color format -'hex','rgb', or'hsl'. Default:'hex'
Returns: Random color string
color(); // '#a3c2f0'
color('hex'); // '#ff5733'
color('rgb'); // 'rgb(123, 45, 200)'
color('hsl'); // 'hsl(240, 70%, 50%)'id(prefix)
Generates a unique ID with optional prefix.
Parameters:
prefix(optional): String to prefix the ID with
Returns: Unique ID string
id(); // 'a4x9m2p7n5q3'
id('user'); // 'user_k8j3m9x2'
id('order'); // 'order_p7a1n5q4'secure(bytes)
Generates a cryptographically secure random hex string.
Parameters:
bytes(optional): Number of random bytes to generate. Default:16
Returns: Secure hex string (length = bytes × 2)
secure(); // 'a3f8c2d4e5b6f7a8c9d0e1f2a3b4c5d6' (32 chars)
secure(8); // 'a3f8c2d4e5b6f7a8' (16 chars)
secure(32); // '...' (64 chars)Use Cases
- 🔐 Generating secure passwords and tokens
- 🎮 Game development (dice rolls, random events)
- 🧪 Testing and mock data generation
- 🆔 Creating unique identifiers and session IDs
- 📧 Verification codes and OTPs
- 🎨 Random UI color schemes
- 📁 Unique filename generation
- 🎯 A/B testing and randomization
- 📊 Synthetic data creation
- 🔑 API key generation
Why Randox?
- Zero Dependencies: No bloat, pure functionality
- Tiny Footprint: Minimal bundle size impact
- Universal: Works in Node.js, browsers, and via CDN
- Simple API: Intuitive names and sensible defaults
- Comprehensive: Everything you need for random generation
- Secure Options: Cryptographically secure functions included
- Well-Tested: Thoroughly tested for reliability
- MIT Licensed: Free for any project
Requirements
- Node.js 18.x or higher
- Modern browsers with ES6+ support
Browser Compatibility
Randox works in all modern browsers:
- Chrome/Edge 60+
- Firefox 55+
- Safari 12+
- Opera 47+
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT © Anurag Anand
Support
If you encounter issues or have questions, please file an issue on the GitHub repository.
Made with ❤️ for developers by Anurag
