Learn to create random numbers and build an interactive dice game!
Random numbers power countless applications:
In this lesson, we'll master random number generation by building a dice game!
Here's the truth: Computers can't actually generate TRULY random numbers!
Why not?
What do they do instead?
They use pseudorandom number generators (PRNGs):
JavaScript's Math.random() is a PRNG that's plenty random for our dice game!
JavaScript's Math.random() generates a random decimal between 0 (inclusive) and 1 (exclusive).
What does \"0 inclusive, 1 exclusive\" mean?
0.000000 is possible0.999999 is closest it gets0 ≤ random < 1Example outputs:
Math.random() → 0.1234567 Math.random() → 0.8765432 Math.random() → 0.0000123 Math.random() → 0.9999876 Math.random() → 0.5555555
Each time you call it, you get a different number!
The problem: Math.random() gives us decimals, but we need whole numbers 1 through 6!
The solution - 4 steps:
Math.floor(Math.random() * 6) + 1
Let's break this down step-by-step:
Step 1: Math.random()
0.0 to 0.999...0.7382Step 2: Multiply by 6
Math.random() * 60.0 to 5.999...0.7382 * 6 = 4.4292Step 3: Math.floor() - Round DOWN
Math.floor(Math.random() * 6)0, 1, 2, 3, 4, or 5Math.floor(4.4292) = 4Step 4: Add 1
Math.floor(Math.random() * 6) + 11, 2, 3, 4, 5, or 64 + 1 = 5Visual breakdown:
0.000 - 0.166 → 0 → 1 0.167 - 0.333 → 1 → 2 0.334 - 0.500 → 2 → 3 0.501 - 0.667 → 3 → 4 0.668 - 0.834 → 4 → 5 0.835 - 0.999 → 5 → 6
JavaScript has three rounding functions:
Math.floor() - Always rounds DOWN
Math.floor(4.1) = 4 Math.floor(4.5) = 4 Math.floor(4.9) = 4
Think: Floor is the ground, go DOWN to reach it!
Math.ceil() - Always rounds UP
Math.ceil(4.1) = 5 Math.ceil(4.5) = 5 Math.ceil(4.9) = 5
Think: Ceiling is above, go UP to reach it!
Math.round() - Rounds to NEAREST
Math.round(4.1) = 4 Math.round(4.5) = 5 Math.round(4.9) = 5
Think: Standard rounding - .5 and above goes up!
⚠️ Why we use floor() for dice:
A dice display needs:
CSS properties for a good dice:
width: 120px; // Square size height: 120px; font-size: 80px; // Big number font-weight: bold; display: flex; // Center the number align-items: center; // Vertically justify-content: center; // Horizontally border-radius: 20px; // Rounded corners box-shadow: ...; // 3D depth effect
How do we create a rolling animation effect?
Use setInterval() to repeatedly run code at timed intervals!
Basic syntax:
setInterval(function() {\n // This code runs repeatedly\n}, 100); // Every 100 milliseconds
For a dice rolling animation:
Why this creates excitement:
Stopping the interval:
const interval = setInterval(/* ... */);\n\n// Later, to stop:\nclearInterval(interval);
What makes the two-dice game interesting?
Special roll messages:
Implementation with if statements:
if (dice1 === dice2) {\n message = \"DOUBLES!\";\n} else if (total === 7) {\n message = \"LUCKY 7!\";\n} else if (total === 12) {\n message = \"BOXCARS!\";\n}
Progress through 5 exercises to create a complete dice game:
💡 This lesson teaches random numbers - a concept you'll use in EVERY game you build!
Goal: Create a function that returns a random number from 1 to 6.
Use Math.random() and Math.floor() to generate the dice roll.
Math.floor(Math.random() * 6) + 1. This ensures you get whole numbers from 1 to 6. Test it multiple times to see different results!
Goal: Show the dice result on the screen with nice formatting.
Create HTML to display the rolled number in a styled dice box.
document.getElementById().textContent to update the display. The dice box should show the number, and result text should say "You rolled a X!"
Goal: Add a button that rolls the dice when clicked.
Create a button with an onclick event that triggers the roll function.
Goal: Make the dice spin when rolling for a more realistic effect!
Add CSS animations and JavaScript timing to create a rolling animation.
setInterval() to rapidly change the dice number, then clearInterval() to stop after a few cycles. Disable the button during animation to prevent spam clicking!
Final Challenge: Create a game with two dice that shows the total score!
Roll both dice together and display the sum with special messages for doubles and lucky 7s!