Math.random()
JavaScript Math.random()
Math.random() returns a random number between 0 (included) and 1 (excluded):
0.5602783686247255
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Math.random() returns a random number between 0 (included) and 1 (excluded):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.random();
</script>
</body>
</html>
JavaScript Random Integers
Math.random()
used with Math.floor()
can be used to return random integers.
JavaScript Math
Math.floor(Math.random() * 10) returns a random integer between 0 and 9 (both included):
7
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 10) returns a random integer between 0 and 9 (both
included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
Math.floor(Math.random() * 10);
</script>
</body>
</html>
No comments:
Post a Comment