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

JavaScript Random

The document discusses random number generation in JavaScript. It explains that Math.random() returns a random number between 0 (inclusive) and 1 (exclusive). It shows how to generate random integers by using Math.floor() with Math.random() and multiplying by the desired range. It provides examples of generating random integers between 0-9, 0-10, 0-99, 0-100, 1-10, and 1-100. Finally, it proposes a proper random function that can generate a random number between a given min and max range.

Uploaded by

oussama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

JavaScript Random

The document discusses random number generation in JavaScript. It explains that Math.random() returns a random number between 0 (inclusive) and 1 (exclusive). It shows how to generate random integers by using Math.floor() with Math.random() and multiplying by the desired range. It provides examples of generating random integers between 0-9, 0-10, 0-99, 0-100, 1-10, and 1-100. Finally, it proposes a proper random function that can generate a random number between a given min and max range.

Uploaded by

oussama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1/16/2021 JavaScript Random

w3schools.com LOG IN

  HTML CSS MORE  EXERCISES   

JavaScript Random
❮ Previous Next ❯

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

Example
Math.random(); // returns a random number

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.

https://www.w3schools.com/js/js_random.asp 1/8
1/16/2021 JavaScript Random

Example

Math.floor(Math.random() * 10); // returns a random integer from 0 to 9

Try it Yourself »

Example

Math.floor(Math.random() * 11); // returns a random integer from 0 to 10

Try it Yourself »

Example

Math.floor(Math.random() * 100); // returns a random integer from 0 to 99

Try it Yourself »

Example

Math.floor(Math.random() * 101); // returns a random integer from 0 to 100

Try it Yourself »

Example

Math.floor(Math.random() * 10) + 1; // returns a random integer from 1 to 10

https://www.w3schools.com/js/js_random.asp 2/8
1/16/2021 JavaScript Random

Try it Yourself »

Example
Math.floor(Math.random() * 100) + 1; // returns a random integer from 1 to 100

Try it Yourself »

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;
}
https://www.w3schools.com/js/js_random.asp 3/8
1/16/2021 JavaScript Random

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 »

❮ Previous Next ❯

https://www.w3schools.com/js/js_random.asp 4/8

You might also like