EN
JavaScript - Math.random() method example
10
points
The Math.random()
function returns floating-point, pseudo-random number between range [0,1), 0 (inclusive) and 1 (exclusive). Based on this function we are able to get random number in range as we can see on below examples.
Simple usage example:
// ONLINE-RUNNER:browser;
console.log( Math.random() ); // 0.9100486004606172
console.log( Math.random() ); // 0.8630710741089413
console.log( Math.random() ); // 0.8052253695967542
function randomInt(min, max) {
return min + Math.floor((max - min) * Math.random());
}
function randomFloat(min, max) {
return min + (max - min) * Math.random();
}
console.log( randomInt( 10, 20) ); // 12
console.log( randomInt(-10, 10) ); // -4
console.log( randomFloat( 10, 20) ); // 14.514897223860018
console.log( randomFloat(-10, 10) ); // -6.645075993092653
1. Documentation
Syntax | Math.random() |
Parameters | This method does not take any arguments. |
Result | Float number value (primitive value). |
Description | random is static method that returns random float number from range <0, 1) - inclusive 0 and exclusive 1. |
2. Custom random method examples
2.1. Random float in range example (exclusive max value)
This example shows how to random number with exclusive max. randomzeFloat()
method is overridden in following ways:
randomzeFloat()
- generates numbers in range0
toNumber.MAX_VALUE
(exclusive),randomzeFloat(max)
- generate numbers in range0
tomax
(exclusive) -max
value must be positive,randomzeFloat(min, max)
- generates numbers in rangemin
tomax
(exclusive).
// ONLINE-RUNNER:browser;
/*
inclusive min (result can be equal to min value)
exclusive max (result won't be equal to max value)
*/
function randomizeFloat(min, max) {
if (max == null) {
if (min <= 0) {
throw new Error('Max value must be positive.');
}
max = (min == null ? Number.MAX_VALUE : min);
min = 0.0;
}
if (min >= max) {
throw new Error("Incorrect arguments.");
}
return min + (max - min) * Math.random();
}
// Example:
console.log(randomizeFloat()); // 1.67319916301163e+308
console.log(randomizeFloat(5)); // 2.7593705936801918
console.log(randomizeFloat(10, 80)); // 37.54521514384005
console.log(randomizeFloat(-50, 50)); // -30.632843429520975
2.2. Random float in range example (inclusive max value)
// ONLINE-RUNNER:browser;
// Generates values from <0, 1>
function randomizeValue() {
var value = (1 + 10E-16) * Math.random();
if (value > 1.0) {
return 1.0;
}
return value;
}
/*
inclusive min (result can be equal to min value)
inclusive max (result can be equal to min value)
*/
function randomizeFloat(min, max) {
if(max == null) {
max = (min == null ? Number.MAX_VALUE : min);
min = 0.0;
}
if(min >= max) {
throw new Error("Incorrect arguments.");
}
return min + (max - min) * randomizeValue();
}
// Example:
console.log(randomizeFloat()); // 1.1960373039711962e+308
console.log(randomizeFloat(5)); // 0.7663988388633522
console.log(randomizeFloat(10, 80)); // 67.81113931017913
console.log(randomizeFloat(-50, 50)); // -13.713816892801674
2.3. Random integer in range example (exclusive max value)
// ONLINE-RUNNER:browser;
/*
inclusive min (result can be equal to min value)
exclusive max (result won't be equal to max value)
*/
function randomizeInteger(min, max) {
if(max == null) {
max = (min == null ? Number.MAX_SAFE_INTEGER : min);
min = 0;
}
min = Math.ceil(min); // inclusive min
max = Math.floor(max); // exclusive max
if(min > max - 1) {
throw new Error("Incorrect arguments.");
}
return min + Math.floor((max - min) * Math.random());
}
// Example:
console.log(randomizeInteger()); // 5547382624322139
console.log(randomizeInteger(5)); // 3
console.log(randomizeInteger(10, 80)); // 62
console.log(randomizeInteger(-50, 50)); // -8
2.4. Random integer in range example (inclusive max value)
// ONLINE-RUNNER:browser;
/*
inclusive min (result can be equal to min value)
inclusive max (result can be equal to min value)
*/
function randomizeInteger(min, max) {
if(max == null) {
max = (min == null ? Number.MAX_SAFE_INTEGER : min);
min = 0;
}
min = Math.ceil(min); // inclusive min
max = Math.floor(max); // exclusive max
if(min > max - 1) {
throw new Error("Incorrect arguments.");
}
return min + Math.floor((max - min + 1) * Math.random());
}
// Example:
console.log(randomizeInteger()); // 5918572174489812
console.log(randomizeInteger(5)); // 5
console.log(randomizeInteger(10, 80)); // 60
console.log(randomizeInteger(-50, 50)); // -15
References
- Random number generation - Wikipedia
- Pseudorandom number generator - Wikipedia
- List of random number generators - Wikipedia
Post thumbnail