JavaScript
Random Number Generation
Generate random numbers and random strings within a range.
By EZ4Code Team
randomrandom
Code
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomStr(len = 8) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
return Array.from({ length: len }, () => chars[Math.floor(Math.random() * chars.length)]).join("");
}
function randomColor() {
return "#" + Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, "0");
}Explanation
Provides multiple random generation methods for integers, strings, colors, etc.
More JavaScript Snippets
Array Map Filter Reduce
Chain map, filter, reduce, find, some, and every on arrays.
Array Deduplication
Deduplicate an array using Set.
Deep Clone
Deep clone objects, supporting common data types.
Debounce Function
Wait a period of time after an event triggers before executing; reset the timer if triggered again during the wait.
Throttle Function
Limit a function to execute at most once within a time interval.
Promise.all Concurrency Control
A Promise executor with concurrency limit.