JavaScript Random Numbers

JavaScript: Random Numbers

In many games and apps, things change or happen by surprise. Maybe a game rolls a dice, picks a random color, or hides a treasure in a different spot each time. This is all done using random numbers. In JavaScript, you can easily create random numbers using a few simple tools. Let’s explore how it works.

What Is Math.random()?

JavaScript has a built-in tool called Math.random(). It gives you a new random number every time you call it. This number is always between 0 (included) and 1 (not included). That means you might get 0.3782, 0.0091, or 0.9999, but you’ll never get 1 or more.

console.log(Math.random());

Every time you run this code, it prints a different number between 0 and just below 1. This is useful, but we often want to get whole numbers or numbers in a specific range. Let’s learn how to do that next.

Getting Whole Numbers

Games and other apps usually need whole numbers, not decimals. For example, a dice doesn’t land on 3.7—it lands on 3 or 4. To get a whole number from a random decimal, we use a function called Math.floor().

Math.floor() removes the decimal part and keeps only the whole number.

let number = Math.floor(Math.random() * 10);
console.log(number);

This code gives a whole number from 0 to 9. Here’s how it works:

  • Math.random() gives a number from 0 to just below 1.
  • Multiplying by 10 makes the range from 0 to just below 10.
  • Math.floor() removes the decimal, giving a number like 0, 1, 2, …, or 9.

If you want a number from 1 to 10 instead, you simply add 1 after rounding.

let number = Math.floor(Math.random() * 10) + 1;
console.log(number);

Now the result will be between 1 and 10.

Getting Numbers Between Any Two Values

Sometimes, you want a random number between any two numbers, like from 50 to 100 or from 5 to 20. You can use this formula:

Math.floor(Math.random() * (max - min + 1)) + min;

This formula works like this:

  • max - min + 1 gives you the size of the range.
  • Math.random() chooses a decimal in that size.
  • Math.floor() makes it a whole number.
  • Adding min moves the result into your desired range.

Example: Random number between 50 and 100

let min = 50;
let max = 100;
let number = Math.floor(Math.random() * (max - min + 1)) + min;

console.log(number);

This will print numbers like 55, 62, 100, or any other number between 50 and 100.

Real-World Examples

Let’s look at how random numbers are used in real projects.

Example 1: Rolling a Dice

let dice = Math.floor(Math.random() * 6) + 1;
console.log("You rolled a " + dice);

This code simulates a dice roll. Since a dice has 6 sides, we multiply by 6 to get values from 0 to 5. Adding 1 shifts the result to be between 1 and 6.

Example 2: Picking a Random Color

let colors = ["red", "blue", "green", "yellow", "purple"];
let index = Math.floor(Math.random() * colors.length);
let randomColor = colors[index];

console.log("Your random color is " + randomColor);

Here we use a random number to pick one item from a list of colors. colors.length gives us the number of colors, and we use that to get a random index.

Example 3: Number Guessing Game

let secretNumber = Math.floor(Math.random() * 10) + 1;
let guess = 5; // pretend the user guessed 5

if (guess === secretNumber) {
  console.log("Correct!");
} else {
  console.log("Wrong! The number was " + secretNumber);
}

This code sets a secret number between 1 and 10, and compares it with a guess. If the guess is correct, it shows a success message. Otherwise, it shows the correct number.

Common Mistakes to Avoid

One common mistake is forgetting to use Math.floor(), which leads to decimal numbers instead of whole numbers. Another mistake is getting the range wrong — for example, multiplying by 10 but expecting results from 1 to 10. Always double-check your formula and test it to make sure it works as expected.

Conclusion

Here’s what we’ve learned:

  • Math.random() gives a decimal between 0 and 1.
  • You can multiply and round it to get whole numbers.
  • Use Math.floor() to remove decimals.
  • You can create numbers in any range using a simple formula.
  • Random numbers are great for games, fun effects, and useful tools.
Scroll to Top