Introduction to JavaScript Math.random()
function
The Math
object is a built-in object in JavaScript that provides a variety of mathematical functions and constants. These functions and constants can be used to perform common mathematical operations, such as calculating the square root of a number, or determining the value of pi.
In this article, we will discuss how to use the random
function of the Math
object.
Using the Math.random
function
One of the most commonly used functions of the Math
object is the random
function, which is used to generate a random number between 0 (inclusive) and 1 (exclusive).
Generating random numbers between 0 and 1
For example, the random
function is called on the Math
object, and it returns a random number between 0 and 1. The result is then assigned to the randomNumber
variable and logged to the console.
let randomNumber = Math.random();
console.log(randomNumber);
Output
0.762643325697278
Generating random numbers between specific range
The Math.random
function is useful for generating random numbers in your JavaScript code, but it has some limitations. For example, the random
function always returns a number between 0 and 1, and it does not allow you to specify a range for the random number. This means that if you want to generate a random number between 10 and 20, for example, you will need to use some additional math to convert the number returned by the random
function into the desired range.
To generate a random number within a specific range using the random
function, you can use the following formula:
let min = 10;
let max = 20;
let randomNumber = Math.random() * (max - min) + min;
console.log(randomNumber);
Output
11.344495421025178
In this example, the random
function is used to generate a random number between 0 and 1, and then the result is multiplied by the difference between the max
and min
values (in this case, 10). The result is then added to the min
value to shift the range of the random number from 0 to 10 to the desired range of 10 to 20.
Generating random numbers with specific decimal places
In addition to generating random numbers within a specific range, the random
function can also be used to generate random numbers with a specific number of decimal places. For example:
let randomNumber = Math.random().toFixed(2);
console.log(randomNumber);
Output
0.19
In this example, the Math.random
function is used to generate a random number between 0 and 1, and then the toFixed
method is called on the result to specify that the number should have 2 decimal places. The toFixed
method returns a string representation of the number with the specified number of decimal places, and the result is then logged to the console.
Summary
In summary, the random
function of the Math
object is a built-in function in JavaScript that is used to generate random numbers between 0 (inclusive) and 1 (exclusive). This function can be used on its own to generate random numbers within this range, or it can be combined with additional math to generate random numbers within a specific range or with a specific number of decimal places. The Math.random
function is a useful tool for generating random numbers in your JavaScript code.
References
Math.random() - JavaScript | MDN (mozilla.org)
Number.prototype.toFixed() - JavaScript | MDN (mozilla.org)