Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

JavaScript Random

Uploaded by

Gil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

JavaScript Random

Uploaded by

Gil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

H T M L C S S J A V A S C R I P T S Q L P Y T H O N J A V A P H P H O W T O W 3 .

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 »

Math.random() always returns a number lower than 1.

JavaScript Random Integers


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.

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

A Proper Random Function


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):

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.

You might also like