The Math.random in JavaScript :- Geniusofstudent

The Math.random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).






Code:-


  • <!DOCTYPE html> 

    1. <html> 
    2.  
    3. <head> 
    4.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
    5. </head> 
    6.  
    7. <body> 
    8.  <h1>Math.random()</h1> 
    9.  
    10.  <input type="button" onclick="rnd();" value="Generate Random Number"> 
    11.  
    12.  <div id="d1"></div> 
    13.  <div id="d2"></div> 
    14.  
    15.  <script> 
    16.   function rnd() 
    17.   { 
    18.  
    19.    document.getElementById("d1").innerHTML = "random 0 to 1 : " + Math.random(); 
    20.    document.getElementById("d2").innerHTML = "random 1 to 100 : " + (Math.floor(Math.random() * 100) + 1); 
    21.  
    22.   } 
    23.  </script> 
    24.  
    25. </body> 
    26.  
    27. </html>
    The Math.random in JavaScript :- Geniusofstudent The Math.random in JavaScript :- Geniusofstudent Reviewed by Network security on July 07, 2019 Rating: 5

    No comments:

    Useful Information

    Powered by Blogger.