Understanding Math.random()
Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). This is the foundation for creating all kinds of random values in your programs!
Basic Usage
⚡ javascript
1
2
3
4
5
6
7
// Generates a random number: 0 <= number < 1
console.log(Math.random()) // 0.7234891...
console.log(Math.random()) // 0.3891234...
console.log(Math.random()) // 0.9123456...
// Each time you call it, you get a different number!
console.log(Math.random()) // 0.1567890...Key Points:
- Returns a decimal between 0 and 0.999...
- Never returns exactly 1
- Different every time you call it
- Perfect for games, simulations, and randomness
Visual Range:
0
Math.random() can be anywhere here
1Includes 0, but never quite reaches 1