JavaScript Random Number16 Feb 2025 | 4 min read We will understand the JavaScript random number in this article. Let us first understand the Math object. What is a Math object?JavaScript consists of a built-in static object called "Math". This object permits you to do mathematical calculations. It consists of some built-in methods that help to perform mathematical operations. Syntax: We can get the random number in JavaScript with the help of the following method:
We will comprehend these methods properly with the help of demonstrations. Math.random()This method is utilized to return a random number between 0 and 1 which means it always returns a number smaller than 1. Syntax: Demonstrations:We will look at the demonstrations to generate a random number utilizing the Math.random() method. Demo 1: We will get a random decimal number between 0 and 1 utilizing the Math.random() method. Code: Output: We can witness the output below that has a decimal random number between 0 and 1. Demo 2: We will get a random whole number which will be a whole number utilizing Math.random() in this demo. Code: Output: We can witness the output that consists of a random whole number. Demo 3: We will get a random number between a specific range utilizing the Math.random() method. Code: Output: We can witness in the output that the random numbers are generated between specific ranges 5 & 10 and 1 & 50. Demo 4: We will get a random integer number within the range utilizing the Math.random() method. Code: Output: We can witness the output that consists of a random integer number between specific ranges. Demo 5: We will generate a random number with the specified decimal places utilizing the Math.random() method. Code: Output: We can witness the output that consists of a random number with two decimal places. Math.floor()It is used to round a number down to the nearest integer. It returns a random number between 0 and 1 which means a number that is always less than 1. We use Math.floor() with Math.random() to generate a random number. Demo 1: We will get a random number without a decimal between 0 and 10 utilizing the Math.floor() method. Code: Output: We can witness the output that consists of a random number without a decimal between 0 and 10. Demo 2: We will get a random number without a decimal between any two numbers input by the users. Code: Output: We can witness the output that shows a random number without a decimal between any two numbers input by the users. Demo 4: We will get inclusive random numbers between a specific range utilizing the Math.ceil(), Math.floor(), and Math.random() methods. Code: Output: We can witness the output that consists of a random number without a decimal between any two numbers. Conclusion:We have understood the JavaScript random number in this article. We have understood how to generate a random number with the help of demonstrations. We can utilize Math.random() and Math.floor() methods to do so. Next TopicJavascript-react-ides |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India