Generate Javascript Random Number between 1 and 10

Generate Javascript Random Number between 1 and 10
<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>
<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>

Two ways to generate a javascript random number between 1 and 10

  • Using Math.random() function with the help of
  • Using the javascript function with the help of minimum and maximum numbers.
<html> 
<head>
<title>Javascript random number example</title>
</head>
<body>
<h3>Random number between 1 and 10</h3>
<p id="random_number"></p>
<script type="text/javascript"> document.getElementById("random_number").innerHTML = Math.floor(Math.random()*10);
</script>
</body>
</html>
<html> 
<head>
<title>Javascript random number example</title>
</head>
<body>
<button type="button" onclick="genearteRandomNumber()">Click here to generate random number between 1 and 10</button>
<p id="random_number_using_function"></p>
<script type="text/javascript">
function genearteRandomNumber()
{
document.getElementById("random_number_using_function").innerHTML = Math.floor(Math.random()*(10-1))+1;
}
</script>
</body>
</html>

--

--

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bipsmedium

Bipsmedium

25 Followers

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.