Random Numbers Example0%

Random Numbers Example

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

Generating Random Numbers and the Dice Game Example

Generating random numbers is crucial for games, unique ID generation, simulations, password generators, and lottery drawings. JavaScript provides Math.random() as its pseudo-random number generator.


1. How Math.random() Works

Math.random() returns a floating-point, pseudo-random number in the range: $$0 \le \text{result} < 1$$

  • It can return 0 (inclusive).
  • It approaches 0.999999..., but NEVER returns 1!
JavaScript
console.log(Math.random()); // e.g. 0.428172948192
console.log(Math.random()); // e.g. 0.892019482711

2. Generating Random Integers in a Range

To transform that float into a useful integer range:

Formula 1: Random Integer from 0 to $N - 1$:

JavaScript
// Random integer from 0 to 9:
const rand0to9 = Math.floor(Math.random() * 10);

Formula 2: The Universal Min-Max Formula (Inclusive):

To generate a random integer between min and max (inclusive):

JavaScript
function getRandomInt(min, max) {
// Enforce whole numbers:
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
JavaScript
// Random number between 1 and 100:
const roll100 = getRandomInt(1, 100);
 
// Random number between -10 and 10:
const temp = getRandomInt(-10, 10);

3. Hands-On Project: Two-Player Dice Game

Let's build a practical script that simulates rolling two six-sided dice between Player 1 and Computer:

JavaScript
function rollDie() {
// Returns integer between 1 and 6 inclusive
return Math.floor(Math.random() * 6) + 1;
}
 
function playDiceGame() {
const playerRoll = rollDie();
const computerRoll = rollDie();
 
console.log(`🎲 You rolled: ${playerRoll}`);
console.log(`🎲 Computer rolled: ${computerRoll}`);
 
if (playerRoll > computerRoll) {
console.log("🏆 Victory! You won the round!");
} else if (computerRoll > playerRoll) {
console.log("💥 Defeat! The computer won this time.");
} else {
console.log("🤝 It's a draw! Roll again.");
}
}
 
playDiceGame();

4. Picking a Random Item from an Array

Combine Math.random() with array.length to select a random element:

JavaScript
const colors = ["Crimson", "RoyalBlue", "Emerald", "Gold", "Violet"];
 
function getRandomItem(arr) {
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}
 
console.log(`Lucky color: ${getRandomItem(colors)}`);

Interactive Quiz

Practice Quiz

Question 1
What is the exact output range of Math.random() in JavaScript?
Question 2
What formula generates a random integer from 1 to 6 (like rolling a standard die)?
Question 3
How do you select a random element from an array const list = ["A", "B", "C", "D"]?
Question 4
Why is Math.floor() preferred over Math.round() when generating random integer ranges?
Question 5
What is the maximum value that Math.floor(Math.random() * 10) can return?
Next Lesson

Selecting Elements: getElementById, querySelector

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext