Loading Please Wait...
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive). Math.random() always returns a number lower than 1.
Math.random();
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.
Math.floor(Math.random() * 10); // integer from 0 to 9
Math.floor(Math.random() * 11); // integer from 0 to 10
Math.floor(Math.random() * 100); // integer from 0 to 99
Math.floor(Math.random() * 101); // integer from 0 to 100
Math.floor(Math.random() * 10) + 1; // integer from 1 to 10
Math.floor(Math.random() * 100) + 1; // integer from 1 to 100
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 and max.
// min (included) and max (excluded)
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
// both min and max (included)
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved