Open In App

What is the use of the Math.random Function in JavaScript ?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Math.random() function in JavaScript is used to generate a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive).

Example 1: Here, Math.random() is used to generate a random number.

Javascript




let randomNumber = Math.random();
// Outputs a random number between 0 and 1 (excluding 1)
console.log(randomNumber);


Output

0.7620477508766035

Example 2: Here, Math.random() generates a number between 0 and 1, * 10 scales it to a range between 0 and 10, Math.floor() rounds it down to the nearest integer, and + 1 shifts the range to be between 1 and 10.

Javascript




let randomInteger = Math.floor(Math.random() * 10) + 1;
// Outputs a random integer between 1 and 10
console.log(randomInteger);


Output

10

Previous Article
Next Article

Similar Reads

What is the use of the Math.random() function in JavaScript ?
The Math.random() function is used to generate a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). Syntax:Math.random();Example: Here, Math.random() generates a random number, which could be any floating-point value between 0 (inclusive) and 1 (exclusive). This random number is then stored in the variable randomNumber, an
1 min read
JavaScript Math random() Method
The JavaScript Math.random() function gives you a random number between 0 and just under 1. You can use this number as a base to get random numbers within any range you want. Now, let's see the syntax to use the JavaScript random method along with multiple examples to easily understand how we can generate random numbers using the random method. Syn
3 min read
What is the use of Math object in JavaScript ?
Math is an inbuilt object that has attributes and methods for mathematical functions and constants. It’s not a function object. Math object works with the Number type. The Math object does not have a constructor. All properties and methods of Math are fixed/static. The cosine function is known as Math.cos(y) while the constant pi is known as Math.P
4 min read
JavaScript Math.round( ) function
JavaScript Math.round( ) function is used to round the number passed as a parameter to its nearest integer. Syntax: Math.round(value) Parameters: value: The number to be rounded to its nearest integer. Example 1: Rounding Off a number to its nearest integer To round off a number to its nearest integer, the math.round() function should be implemente
2 min read
JavaScript Math cbrt() Function
The Javascript Math.cbrt() function is used to find the cube root of a number. The cube root of a number is denoted as Math.cbrt(x)=y such that y^3=x. Syntax: Math.cbrt(number) Parameters: This function accepts a single parameter. number: The number whose cube root you want to know. Return Value: It returns the cube root of the given number. The be
1 min read
JavaScript Math.clz32() Function
The Math.clz32( ) function in JavaScript returns the number of leading zero bits in the 32-bit binary representation of a number. Clz32 stands for Count Leading Zeros 32. If the passed parameter is not a number, then it will be converted into a number first and then converted to a 32-bit unsigned integer. If the converted 32-bit unsigned integer is
2 min read
JavaScript Math.atanh() Function
The Javascript Math.atanh() function is an inbuilt function in JavaScript that is used to get the hyperbolic arctangent of a number. The hyperbolic arctangent is known by many names such as hyperbolic inverse tangent and atanh. It is the inverse of the hyperbolic tangent function i.e, The inverse hyperbolic tangent of any value says x is the value
2 min read
JavaScript Math.ceil( ) function
Math.ceil() function in JavaScript is used to round the number passed as a parameter to its nearest integer in an Upward direction of rounding i.e. towards the greater value. Syntax:Math.ceil(value);Parameters: This function accepts a single parameter as the number to be rounded to its nearest integer in the upward rounding method. Return Value: Re
2 min read
JavaScript Math cosh() Method
JavaScript Math.cosh() method is used to calculate the value of the hyperbolic cosine of a number. Syntax: Math.cosh(x) Parameter: This method accepts a single parameter as mentioned above and described below: x: This parameter holds a number for which the value of hyperbolic cosine is going to be calculated. Returns: It returns the calculated valu
3 min read
JavaScript Math abs() Method
Javascript Math.abs() method is used to return the absolute value of a number. It takes a number as its parameter and returns its absolute value. Syntax: Math.abs(value)Parameters: This method accepts a single parameter as mentioned above and described below: value: The number whose absolute value is to be found is passed as the parameter to this f
2 min read