Languages
[Edit]
EN

JavaScript Math.random() explain the difference between Math.ceil() vs Math.floor() vs Math.round()

17 points
Created by:
Zayyan-Todd
860

1. Random number

In JavaScript to generate a random number we use Math.random() and it returns numbers between 0 and 1.

// ONLINE-RUNNER:browser;

console.log(Math.random()); // 0.587777376294869
console.log(Math.random()); // 0.2197126264732836
console.log(Math.random()); // 0.9785932144070053

2. Random number between 0 and 10 - floating point

If we want to get a random number between 0 and 10 we just need to multiply the result of Math.random() by 10 and round the result.

// ONLINE-RUNNER:browser;

console.log(Math.random() * 10); // 9.357923991790457
console.log(Math.random() * 10); // 3.5154493888867466
console.log(Math.random() * 10); // 5.1469888802577035

3. Random number between 0 and 10 - integer

Still, as we can see the numbers are floating-point and usually we want to have integers. The solution is pretty simple and now with help comes 3 methods how we can achieve it:

  • Math.round()
  • Math.floor()
  • Math.ceil()

4. UsingĀ Math.round()

Math.round() - round to the nearest number (integer e.g. 1,5,10)
e.g.:

  • 1.23 will be rounded to 1
  • 5.92 will be rounded to 6

Example how to generate a random number between 0 and 10 with Math.round().

// ONLINE-RUNNER:browser;

console.log(Math.round(1.23)); // 1
console.log(Math.round(5.92)); // 6

console.log(Math.round(Math.random() * 10)); // 2
console.log(Math.round(Math.random() * 10)); // 9
console.log(Math.round(Math.random() * 10)); // 5

5. Using Math.floor()

Math.floor() - round DOWN to the nearest number
e.g.:

  • 1.23 will be rounded to 1
  • 5.92 will be rounded to 5

Example how to generate random numbers between 0 and 10 with Math.floor().

// ONLINE-RUNNER:browser;

console.log(Math.floor(1.23)); // 1
console.log(Math.floor(5.92)); // 5

console.log(Math.floor(Math.random() * 10)); // 3
console.log(Math.floor(Math.random() * 10)); // 1
console.log(Math.floor(Math.random() * 10)); // 6

6. UsingĀ Math.ceil()

Math.ceil() - round UP to the nearest number
e.g.:

  • 1.23 will be rounded to 2
  • 5.92 will be rounded to 6

Example how to generate random numbers between 0 and 10 with Math.ceil().

// ONLINE-RUNNER:browser;

console.log(Math.ceil(1.23)); // 2
console.log(Math.ceil(5.92)); // 6

console.log(Math.ceil(Math.random() * 10)); // 5
console.log(Math.ceil(Math.random() * 10)); // 7
console.log(Math.ceil(Math.random() * 10)); // 3

References

  1. Math.random methodĀ - Mozilla Docs
  2. Math.roundĀ methodĀ - Mozilla Docs
  3. Math.floorĀ methodĀ - Mozilla Docs
  4. Math.ceilĀ methodĀ - Mozilla Docs

Post thumbnail

JavaScript Math.random() explain the difference between Math.ceil() vs Math.floor() vs Math.round() - intro image
Intro image - Math.random()
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - math (popular problems)

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join