Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to generate a random number in JavaScript?
JavaScript's Math.random() method is the built-in way to generate random numbers. It returns a floating-point number between 0 (inclusive) and 1 (exclusive), which can be scaled to any desired range.
The Math.random() function returns a pseudo-random number in the range [0, 1) with uniform distribution. The implementation generates the seed automatically, and users cannot modify it.
Basic Math.random() Usage
Syntax
Math.random()
Return Value
A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).
Example
<!DOCTYPE html>
<html>
<head>
<title>Random Number Generation</title>
</head>
<body>
<script>
function generateBasicRandom() {
return Math.random();
}
document.write("Basic random number: " + generateBasicRandom());
</script>
</body>
</html>
Generating Random Integers with Math.floor()
The Math.floor() function rounds numbers down to the nearest integer, making it perfect for generating random integers.
Random Integer Between Min and Max (Exclusive)
<!DOCTYPE html>
<html>
<head>
<title>Random Integer Generation</title>
</head>
<body>
<script>
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
document.write("Random integer between 1 and 10: " + randomInteger(1, 10));
</script>
</body>
</html>
Random Integer Between Min and Max (Inclusive)
<!DOCTYPE html>
<html>
<head>
<title>Random Integer Inclusive</title>
</head>
<body>
<script>
function randomIntegerInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
document.write("Random integer between 1 and 20 (inclusive): " + randomIntegerInclusive(1, 20));
</script>
</body>
</html>
Practical Examples
Random Number in Custom Range
<!DOCTYPE html>
<html>
<head>
<title>Custom Range Random Numbers</title>
</head>
<body>
<script>
function randomInRange(min, max) {
return Math.random() * (max - min) + min;
}
document.write("Random decimal between 5.5 and 10.5: " + randomInRange(5.5, 10.5).toFixed(2));
document.write("<br>");
document.write("Random integer between 50 and 100: " + Math.floor(randomInRange(50, 101)));
</script>
</body>
</html>
Common Use Cases
| Purpose | Formula | Example |
|---|---|---|
| Basic random (0-1) | Math.random() |
0.7234567 |
| Random integer (0 to n-1) | Math.floor(Math.random() * n) |
0-9 for n=10 |
| Random integer (min to max inclusive) | Math.floor(Math.random() * (max-min+1)) + min |
1-10 inclusive |
| Random decimal in range | Math.random() * (max-min) + min |
5.5-10.5 range |
Key Points
-
Math.random()returns values from 0 (inclusive) to 1 (exclusive) - Use
Math.floor()to convert to integers - For inclusive ranges, add 1 to the difference:
(max - min + 1) - Random numbers are pseudo-random, not truly random
- The seed is automatically generated and cannot be set manually
Conclusion
Math.random() is the standard method for generating random numbers in JavaScript. Combined with Math.floor() and proper scaling formulas, it can generate random integers and decimals within any desired range for various applications.
