EN
JavaScript - Math.random() method example
13 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 a random number in range as we can see in the below examples.
Quick solutions:
xxxxxxxxxx
1
console.log( Math.random() ); // 0.9100486004606172
2
console.log( Math.random() ); // 0.8630710741089413
3
console.log( Math.random() ); // 0.8052253695967542
Alternative solution:
xxxxxxxxxx
1
function randomInt(min, max) {
2
return min + Math.floor((max - min) * Math.random());
3
}
4
5
console.log( randomInt( 10, 20) ); // 12
6
console.log( randomInt(-10, 10) ); // -4
xxxxxxxxxx
1
function randomFloat(min, max) {
2
return min + (max - min) * Math.random();
3
}
4
5
console.log( randomFloat( 10, 20) ); // 14.514897223860018
6
console.log( randomFloat(-10, 10) ); // -6.645075993092653
Syntax | Math.random() |
Parameters | This method does not take any arguments. |
Result | Float number value (primitive value). |
Description | random is a static method that returns a random float number from the range <0, 1) - inclusive 0 and exclusive 1. |
This example shows how to random numbers with exclusive max. randomizeFloat()
method is overridden in the following ways:
randomizeFloat()
- generates numbers in a range from0
toNumber.MAX_VALUE
(exclusive),randomizeFloat(max)
- generate numbers in a range from0
tomax
(exclusive) -max
value must be positive,randomizeFloat(min, max)
- generates numbers in a range frommin
tomax
(exclusive).
xxxxxxxxxx
1
/*
2
inclusive min (result can be equal to min value)
3
exclusive max (result won't be equal to max value)
4
*/
5
function randomizeFloat(min, max) {
6
if (max == null) {
7
if (min <= 0) {
8
throw new Error('Max value must be positive.');
9
}
10
max = (min == null ? Number.MAX_VALUE : min);
11
min = 0.0;
12
}
13
if (min >= max) {
14
throw new Error("Incorrect arguments.");
15
}
16
return min + (max - min) * Math.random();
17
}
18
19
20
// Usage example: // Example output:
21
22
console.log(randomizeFloat()); // 1.67319916301163e+308
23
console.log(randomizeFloat(5)); // 2.7593705936801918
24
console.log(randomizeFloat(10, 80)); // 37.54521514384005
25
console.log(randomizeFloat(-50, 50)); // -30.632843429520975
xxxxxxxxxx
1
// Generates values from <0, 1>
2
//
3
function randomizeNumber() {
4
return Math.min(1, 1.0000000000000003 * Math.random());
5
}
6
7
/*
8
inclusive min (result can be equal to min value)
9
inclusive max (result can be equal to min value)
10
*/
11
function randomizeFloat(min, max) {
12
if(max == null) {
13
max = (min == null ? Number.MAX_VALUE : min);
14
min = 0.0;
15
}
16
if(min >= max) {
17
throw new Error("Incorrect arguments.");
18
}
19
return min + (max - min) * randomizeNumber();
20
}
21
22
23
// Usage example: // Example output:
24
25
console.log(randomizeFloat()); // 1.1960373039711962e+308
26
console.log(randomizeFloat(5)); // 0.7663988388633522
27
console.log(randomizeFloat(10, 80)); // 67.81113931017913
28
console.log(randomizeFloat(-50, 50)); // -13.713816892801674
xxxxxxxxxx
1
/*
2
inclusive min (result can be equal to min value)
3
exclusive max (result won't be equal to max value)
4
*/
5
function randomizeInteger(min, max) {
6
if(max == null) {
7
max = (min == null ? Number.MAX_SAFE_INTEGER : min);
8
min = 0;
9
}
10
11
min = Math.ceil(min); // inclusive min
12
max = Math.floor(max); // exclusive max
13
14
if(min > max - 1) {
15
throw new Error("Incorrect arguments.");
16
}
17
18
return min + Math.floor((max - min) * Math.random());
19
}
20
21
// Example usage: // Example output:
22
23
console.log(randomizeInteger()); // 5547382624322139
24
console.log(randomizeInteger(5)); // 3
25
console.log(randomizeInteger(10, 80)); // 62
26
console.log(randomizeInteger(-50, 50)); // -8
xxxxxxxxxx
1
/*
2
inclusive min (result can be equal to min value)
3
inclusive max (result can be equal to min value)
4
*/
5
function randomizeInteger(min, max) {
6
if(max == null) {
7
max = (min == null ? Number.MAX_SAFE_INTEGER : min);
8
min = 0;
9
}
10
min = Math.ceil(min); // inclusive min
11
max = Math.floor(max); // exclusive max
12
if(min > max - 1) {
13
throw new Error("Incorrect arguments.");
14
}
15
return min + Math.floor((max - min + 1) * Math.random());
16
}
17
18
19
// Example usage: // Example output:
20
21
console.log(randomizeInteger()); // 5918572174489812
22
console.log(randomizeInteger(5)); // 5
23
console.log(randomizeInteger(10, 80)); // 60
24
console.log(randomizeInteger(-50, 50)); // -15
- Random number generation - Wikipedia
- Pseudorandom number generator - Wikipedia
- List of random number generators - Wikipedia