JavaScript Random
JavaScript Random
C S S C C + + C # B O O T S T R A P R E A C T M Y S Q L J Q U E R Y E X C E L X M L D J A N G O N U M P Y P A N D A S N O D E J S R T Y P E S C R I P T A N G U L A R G I T P O S T G R E S Q L M O N G O D B A S P A I G O K O T L I N S A S S V U E D S A G E N A I S C I P Y A W S C Y B E R S E C U R I T Y D A T A S C I E N C E
JavaScript Random
❮ PreviousNext ❯
Math.random()
Math.random() returns a random number between 0 (inclusive), and 1
(exclusive):
Example
// Returns a random number:
Math.random();
Try it Yourself »
Example
// Returns a random integer from 0 to 9:
Math.floor(Math.random() * 10);
Try it Yourself »
Example
// Returns a random integer from 0 to 10:
Math.floor(Math.random() * 11);
Try it Yourself »
Example
// Returns a random integer from 0 to 99:
Math.floor(Math.random() * 100);
Try it Yourself »
Example
// Returns a random integer from 0 to 100:
Math.floor(Math.random() * 101);
Try it Yourself »
Example
// Returns a random integer from 1 to 10:
Math.floor(Math.random() * 10) + 1;
Try it Yourself »
Example
// Returns a random integer from 1 to 100:
Math.floor(Math.random() * 100) + 1;
Try it Yourself »
ADVERTISEMENT
Example
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
Try it Yourself »
This JavaScript function always returns a random number between min and
max (both included):
Example
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
Try it Yourself »
❮ PreviousNext ❯
ADVERTISEMENT
COLOR PICKER
ADVERTISEMENT
SPACES
UPGRADE
AD-FREE
NEWSLETTER
GET CERTIFIED
CONTACT US
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
SQL Examples
Python Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
Java Examples
XML Examples
jQuery Examples
Get Certified
HTML Certificate
CSS Certificate
JavaScript Certificate
Front End Certificate
SQL Certificate
Python Certificate
PHP Certificate
jQuery Certificate
Java Certificate
C++ Certificate
C# Certificate
XML Certificate
FORUM ABOUT CLASSROOM
W3Schools is optimized for learning and training. Examples might be simplified to improve reading
and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and
accepted our terms of use, cookie and privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.