The Math.random() method returns a random number from 0 (inclusive) up to but not including 1 (exclusive).
Code:-
<!DOCTYPE html>
Code:-
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- </head>
- <body>
- <h1>Math.random()</h1>
- <input type="button" onclick="rnd();" value="Generate Random Number">
- <div id="d1"></div>
- <div id="d2"></div>
- <script>
- function rnd()
- {
- document.getElementById("d1").innerHTML = "random 0 to 1 : " + Math.random();
- document.getElementById("d2").innerHTML = "random 1 to 100 : " + (Math.floor(Math.random() * 100) + 1);
- }
- </script>
- </body>
- </html>
The Math.random in JavaScript :- Geniusofstudent
Reviewed by Network security
on
July 07, 2019
Rating:
No comments: