Testing•8 min read
Random Data Generators: Complete Guide for Testing
Generate random data for testing and development. Create random strings, numbers, names, colors, and more.
By Andy Pham
Random Data Generators: Complete Guide for Testing
Random data generation is essential for testing, development, and data seeding. Learn to generate various types of test data.
Random Number Generation
Basic Random Number
// Random integer between min and max
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Random float
function randomFloat(min, max, decimals = 2) {
const num = Math.random() * (max - min) + min;
return Number(num.toFixed(decimals));
}
Cryptographic Random
// More secure random
function secureRandomInt(min, max) {
const range = max - min + 1;
const bytes = crypto.getRandomValues(new Uint32Array(1));
return min + (bytes[0] % range);
}
Random Strings
function randomString(length, charset = 'alphanumeric') {
const charsets = {
alphanumeric: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
alpha: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
numeric: '0123456789',
hex: '0123456789ABCDEF',
special: '!@#$%^&*()_+-=[]{}|;:,.<>?'
};
const chars = charsets[charset] || charset;
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
Random Names
const firstNames = ['John', 'Jane', 'Michael', 'Sarah', 'David', 'Emily'];
const lastNames = ['Smith', 'Johnson', 'Williams', 'Brown', 'Jones', 'Davis'];
function randomName() {
const first = firstNames[randomInt(0, firstNames.length - 1)];
const last = lastNames[randomInt(0, lastNames.length - 1)];
return `${first} ${last}`;
}
Random Passwords
function randomPassword(length = 16, options = {}) {
const {
uppercase = true,
lowercase = true,
numbers = true,
special = true
} = options;
let chars = '';
if (uppercase) chars += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (lowercase) chars += 'abcdefghijklmnopqrstuvwxyz';
if (numbers) chars += '0123456789';
if (special) chars += '!@#$%^&*()_+-=';
const array = new Uint32Array(length);
crypto.getRandomValues(array);
return Array.from(array, x => chars[x % chars.length]).join('');
}
Random Colors
function randomHexColor() {
return '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
}
function randomRgbColor() {
return {
r: randomInt(0, 255),
g: randomInt(0, 255),
b: randomInt(0, 255)
};
}
Random Dates
function randomDate(start, end) {
const startTime = start.getTime();
const endTime = end.getTime();
const randomTime = startTime + Math.random() * (endTime - startTime);
return new Date(randomTime);
}
// Random date in 2024
randomDate(new Date(2024, 0, 1), new Date(2024, 11, 31));
Random Data Libraries
- Faker.js - Comprehensive fake data
- Chance.js - Probability-based random
- uuid - Unique identifiers
Try Our Random Generators
Conclusion
Random data generators are essential for testing. Use appropriate randomness for your use case.