Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Basic JavaScript

JS Tutorial JS Introduction JS Where To JS Output

JS Syntax

JS Syntax JS Statements JS Comments JS Variables JS Let JS Const JS Types

JS Operators

JS Operators

JS If Else

JS If Conditions

JS Loops

JS Loops

JS Strings

JS Strings

JS Numbers

JS Numbers

JS Functions

JS Functions

JS Objects

JS Objects

JS Scope

JS Scope

JS Dates

JS Dates

JS Temporal

JS Temporal  New

JS Arrays

JS Arrays

JS Sets

JS Sets

JS Maps

JS Maps

JS Iterations

JS Loops

JS Math

JS Math

JS RegExp

JS RegExp

JS DataTypes

JS Data Types

JS Errors

JS Errors

JS Debugging

JS Debugging

JS Conventions

JS Style Guide

JS Reference

JS Statements

JS Projects

JS Projects New

JS Versions

JS 2026

JS HTML

JS HTML DOM JS Events

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Random

Example

// Returns a random number:
Math.random();
Try it Yourself »

JavaScript Math.random()

Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive):

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

// Return a random integer from 0 to 9 (both included):
Math.floor(Math.random() * 10);
Try it Yourself »

Explained:

Math.random() returns a floating-point number between 0 (inclusive) and 1 (exclusive).

Example outputs: 0.0, 0.237, 0.9999, but never 1.

Math.random() * 10 gives a range from 0 up to but not including 10.

Example possible results: 0.0, 3.5, 9.99, etc.

Math.floor() rounds a number down to the nearest whole integer:

  • 3.5 becomes 3
  • 9.99 becomes 9
  • 0.1 becomes 0

The possible integer results are then 0 through 9 (both inclusive).

In other words, the range is [0, 9].

Example

// Return a random integer from 0 to 10 (both included):
Math.floor(Math.random() * 11);
Try it Yourself »

Example

// Return a random integer from 0 to 99 (both included):
Math.floor(Math.random() * 100);
Try it Yourself »

Example

// Return a random integer from 0 to 100 (both included):
Math.floor(Math.random() * 101);
Try it Yourself »

Example

// Return a random integer between 1 and 10 (both included):
Math.floor(Math.random() * 10) + 1;
Try it Yourself »

Explained:

Math.random() returns a number from 0 (inclusive) up to but not including 1.

Multiplying by 10 gives a number from 0 up to but not including 10.

Adding 1 shifts that range to 1 up to but not including 11.

Math.floor() then rounds down, so you get an integer between 1 and 10.

Example

// Returns a random integer from 1 to 100 (both included):
Math.floor(Math.random() * 100) + 1;
Try it Yourself »

Summary

ExpressionRange fromRange to
Math.random()0<1
Math.random() * 100<10
Math.random() * 1000<100
Math.floor(Math.random() * 10)09


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 integer 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 integer between min and max (both included):

Example

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) + min;
}
Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

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, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->