Random number Math function

Random numbers are required in different scripts like game, dice and many other applications. We can use JavaScript math function random() to generate random number between 0 and 1 ( inclusive of 0 but exclusive of 1 ) .
document.write(Math.random()); // Output 0.6641830555215622
Each time you will get a different value by using this button
Above value is between 0 ( inclusive ) and 1 (exclusive)

We will use Math.floor to get random number within a range.

Random number Between 1 and 10

Output can be equal to 1 but always less than 10 ( not equal to 10 ) , Random integer is also displayed by using Math.floor()
<script>function generate2(){
var my_num=Math.random() *(10-1) + 1;
document.getElementById("d2").innerHTML=my_num;
document.getElementById("d2_1").innerHTML=Math.floor(my_num);
}
</script>
<div id=d2></div>  <div id=d2_1></div>

Random number between 90 & 100 ( both inclusive )

<script>function generate3(){
var my_num=Math.random() *(100-90+1) + 90;
document.getElementById("d3").innerHTML=my_num;
document.getElementById("d3_1").innerHTML=Math.floor(my_num);
}
</script>
<div id=d3></div>  <div id=d3_1></div>

Generate Random integer between two values ( both inclusive)

<script>function generate4(){
var min=40.5348; //Change this value
var max=50.8944; //Change this value
min = Math.ceil(min);
max = Math.floor(max);
  
var my_num=Math.floor(Math.random() * (max - min + 1)) + min;
document.getElementById("d4").innerHTML=my_num;
}
</script>
<div id=d4></div>  

Math.ceil()

By using Function

The return value from this function is random number between 0 and 1. As we have to remove the decimal part to get random numbers between 0 to 10.

We will multiply the random number returned by math.random() by 11 and then take the floor value of it to get the random number. We have used floor value as this function returns the lower integer part only. If we use round function then the value will be adjusted to nearest integer so floor function gives more accurate random number.

Here is the demo of random number generator.

Here is the total code of this demo.

<html>
<head>
<title>(Type a title for your page here)</title>

<script type="text/javascript"> 
function generate(){
var my_num=Math.random();
document.f1.t1.value=(my_num*11);
document.f1.t2.value=Math.floor(my_num*11);
}
</script>

</head>
<body >
<form name=f1>
Random number after multiplying with 11
<input type=text name=t1>
<br>After taking the floor value
<input type=text name=t2>

<input type=button value='Display' onClick='generate()';>
</form>

</body>
</html>
You can read the php random number generator and
ASP random number generator

JavaScript Math Reference
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer