JavaScript Random
Math.random()
The code Math.random() gives you a random number. It can be any number between 0 (including 0) and 1 (not including 1).
Math.random() gives you a number that's less than 1.
JavaScript Random Integers
The code Math.random() can generate random numbers between 0 (inclusive) and 1 (exclusive) when used alone. When combined with Math.floor(), it can be used to produce random whole numbers (integers).
There is no such thing as JavaScript integers.
We're discussing whole numbers, not ones with any decimal points
A Proper Random Function
From the examples shown earlier, it's a smart idea to make a reliable random function for any situation where you need random whole numbers.
This JavaScript function will give you a random number between a specified minimum value (inclusive) and a maximum value (exclusive):
This JavaScript function consistently provides a random number within the given range covers the smallest and largest values.
<!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>
<!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>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 11) returns a random integer between 0 and 10 (both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(Math.random() * 11);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 100)) returns a random integer between 0 and 99 (both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(Math.random() * 100);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor() used with Math.random() * 101 returns a random integer between 0 and 100 (both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(Math.random() * 101);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 10) + 1) returns a random integer between 1 and 10 (both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(Math.random() * 10) + 1;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math</h2>
<p>Math.floor(Math.random() * 100) + 1) returns a random integer between 1 and 100 (both included):</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Math.floor(Math.random() * 100) + 1;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Every time you press the button, a function called getRndInteger(min, max) gives back a random number. This number can be anything from 0 to 9, including both 0 and 9.</p>
<button onclick="document.getElementById('demo').innerHTML = getRndInteger(0,10)">Click Me</button>
<p id="demo"></p>
<script>
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Math.random()</h2>
<p>Each click on the button triggers a function called getRndInteger(min, max), which generates a random number between 1 and 10, including both ends.</p>
<button onclick="document.getElementById('demo').innerHTML = getRndInteger(1,10)">Click Me</button>
<p id="demo"></p>
<script>
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>
</body>
</html>