HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive)
Math.random()
returns a random number between 0 (inclusive), and 1
(exclusive):
// Returns a random number:
Math.random();
Math.random()
always returns a number lower than 1.
Math.random()
used with Math.floor()
can be used to return random integers.
There is no such thing as JavaScript integers.
We are talking about numbers with no decimals here.
// Returns a random integer from 0 to 9:
Math.floor(Math.random() * 10);
// Returns a random integer from 0 to 10:
Math.floor(Math.random() * 11);
// Returns a random integer from 0 to 99:
Math.floor(Math.random() * 100);
// Returns a random integer from 0 to 100:
Math.floor(Math.random() * 101);
// Returns a random integer from 1 to 10:
Math.floor(Math.random() * 10) + 1;
// Returns a random integer from 1 to 100:
Math.floor(Math.random() * 100) + 1;
As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes.
This JavaScript function always returns a random number between min (included) and max (excluded):
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
This JavaScript function always returns a random number between min and max (both included):
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}