Generating a random number within a [min, max] range in JavaScript depends on whether you want a random integer or a random floating-point number, as well as whether the bounds are inclusive. For standard non-cryptographic code (UI picks, dice rolls, sampling), Math.random() is the built-in choice—paired with Math.floor() for inclusive integers.
1. Random Integer Between min and max (Inclusive)
To return a uniform integer from min to max inclusive, normalize both bounds to integers with Math.ceil(min) and Math.floor(max), then scale Math.random() across (maxInt - minInt + 1):
function randomIntInclusive(min, max) {
const minInt = Math.ceil(min);
const maxInt = Math.floor(max);
if (minInt > maxInt) {
throw new RangeError("min must be less than or equal to max");
}
return Math.floor(Math.random() * (maxInt - minInt + 1)) + minInt;
}
console.log(randomIntInclusive(1, 6)); // Integer from 1 to 6 (e.g., 4)
console.log(randomIntInclusive(-5, 5)); // Integer from -5 to 5
2. Random Floating-Point Number and Cryptographic Randomness
When you need continuous decimal values or stronger unpredictability than Math.random()'s PRNG:
-
Random float in
[min, max): ScaleMath.random()(which is in[0, 1)) by(max - min)and addmin:function randomFloat(min, max) { if (min > max) { throw new RangeError(“min must be less than or equal to max”); } return Math.random() * (max - min) + min; }
console.log(randomFloat(0.5, 2.5)); // Float in [0.5, 2.5)
-
Cryptographically stronger random integer (
crypto.getRandomValues): For tokens, security-sensitive sampling, or unbiased uniform distribution without modulo/floating-point bias, usecrypto.getRandomValues()with rejection sampling over aUint32Array:function secureRandomIntInclusive(min, max) { const minInt = Math.ceil(min); const maxInt = Math.floor(max);
if (minInt > maxInt) { throw new RangeError("min must be less than or equal to max"); } const range = maxInt - minInt + 1; const maxUint32 = 0xffffffff; const limit = maxUint32 - (maxUint32 % range); const buffer = new Uint32Array(1); let value; do { crypto.getRandomValues(buffer); value = buffer[0]; } while (value >= limit); return minInt + (value % range);}
3. Common Traps to Avoid
- Using
Math.round(Math.random() * (max - min)) + min:Math.round()makes the outer boundary values (minandmax) roughly half as likely as interior values because half the rounding interval at each end falls outside[min, max]. - Forgetting
+ 1inMath.floor(Math.random() * (max - min + 1)) + min: Without+ 1,maxis never reached for integer ranges. - Using
Math.random()for security or cryptography:Math.random()is not cryptographically secure—usecrypto.getRandomValues()(browser or Node.js) when unpredictability matters.
TL;DR: For an inclusive random integer in [min, max], use Math.floor(Math.random() * (max - min + 1)) + min (avoid Math.round(), which skews endpoint probabilities); for floats use Math.random() * (max - min) + min; and for security-sensitive picks use crypto.getRandomValues().
Are you generating inclusive integers, continuous floats, or security-sensitive random values with crypto.getRandomValues()?
