Member-only story
Generate Javascript Random Number between 1 and 10
Hi friends, in this tutorial you will learn how to generate javascript random number between 1 and 10 which means you have to return all the numbers except 10 which means zero to nine. In order to generate random numbers, you should have the idea of two javascript functions as mentioned below
Math.random( ):- This function always returns the numbers less than one which means it produces the numbers between zero and one or you can say including zero and excluding one.
Example:-
<html>
<head>
<title>Javascript random number example</title>
</head>
<body>
<h3>Below is a random number using Math.random()</h3>
<p id="random"></p>
<script type="text/javascript"> document.getElementById("random").innerHTML = Math.random(); </script>
</body>
</html>
Output:- Below is a random number using Math.random() 0.7600114421439201
Math.floor( ) :- This function returns the number from the left side of a floating point number as shown in the example below
Example:-
<html>
<head>
<title>Javascript random number example</title>
</head>
<body>
<h3>Apply Math.floor() to 1.3</h3>
<p id="floor"></p>
<script type="text/javascript"> document.getElementById("floor").innerHTML = Math.floor(1.3); </script>
</body>
</html>
Output:- Apply Math.floor() to 1.3
Also read, Object in Javascript with example