# JavaScript | Random

The Math.random() function is used to return a floating-point pseudo-random number between range [0,1) , 0 (inclusive) and 1 (exclusive). This random number can be scaled according to the desired range.

Syntax:

`Math.random()`

Example: This example uses Math.random() function to returns a random number.

 ` ` `<``html``> ` ` `  `<``body``> ` `    ``<``h2``>Math.random() ` ` `  `    ``<``p``> ` `        ``Math.random() returns a random ` `        ``number between 0 and 1 ` `    `` ` ` `  `    ``<``p` `id``=``"GFG"``> ` `     `  `    ``` `    ``<``script``> ` `        ``document.getElementById("GFG").innerHTML ` `                ``= Math.random(); ` `    `` ` ` ` ` `  `                     `

Output:

Random Integers: The Math.random() method is used with Math.floor() function to return random integers.

Example:

 ` ` `<``html``> ` ` `  `<``body``> ` `    ``<``center``> ` `        ``<``p``> ` `            ``Math.floor(Math.random() * 11) returns ` `            ``a random integer between 0 and 10  ` `            ``(inclusive): ` `        `` ` `             `  `        ``<``p` `id``=``"demo"``> ` `    `` ` `     `  `    ``` `    ``<``script``> ` `        ``document.getElementById("demo").innerHTML = ` `                    ``Math.floor(Math.random() * 11); ` `    `` ` ` ` ` `  `                     `

Output:

Proper Random Function: JavaScript function always returns a random number between min and max where min is inclusive and max may be inclusive or exclusive both.
Example:

 ` ` `<``html``> ` ` `  `<``body``> ` `    ``<``p``> ` `        ``When you click the button it will ` `        ``give a random value between 0 and ` `        ``9 both inclusive: ` `    `` ` `         `  `    ``<``button` `onclick="document.getElementById('GFG')``.innerHTML` `=  ` `                ``getRndInteger``(0, 10)">Try it ` `                     `  `    ``<``p` `id``=``"GFG"``> ` `     `  `    ``` `    ``<``script``> ` `        ``function getRndInteger(min, max) { ` `            ``return Math.floor(Math.random() * (max - min)) + min; ` `        ``} ` `    `` ` ` ` ` `  `                     `

Output on first click:

Output on second click:

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.