Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Working With Numbers Create a random number

Why not Math.round?

I used Math.round(Math,random()*6); and I got the same result is there a reason why this could be wrong?

3 Answers

All you really need to know is that Math.round rounds to the nearest integer, Math.floor always rounds down, and Math.ceil will always round up. Which one you want to use really depends on the circumstance.

ex:

Math.round(5.6);    // returns 6
Math.floor(5.6);    // returns  5
Math.ceil(5.6);     // returns  6

Thanks Erik, would you recommend using Math.round since it is the mathematically correct method to round up numbers i.e. since 0.1-0.4 = 0 and 0.5 to 0.9 = 1?

Again, it kinda depends. Say I wanted a random number between 1 and 5. I could write Math.round(Math.random()*5) but if the random number is 0.1234 then it will be rounded to 0, which I didn`t want. To get my desired result, I would want to use Math.ceil(Math.random()*5) so the number is always rounded up.

There are other ways I could achieve a random number between 1 and 5, but that is just a little sample of how one rounding method could be a better choice than another in certain situations.

In our case of a die, random means that each outcome has an equal probability of occurring. This means that each outcome (whether we get 1, 2, 3, … , 6) has a 1/6 probability of occurrence.

We have an algorithm that gives a random real number (with a certain precision) in the interval [0, 1). Multiplying each number by 6 gives us a new interval [0, 6) of random numbers.

We can break this interval into six equal-length intervals (each of length 1). Now we need to map these six intervals to the integers 1 - 6. This way, our algorithm will remain random.

Therefore, we have to create a function that will map the interval [0, 1) to the integer 1, the interval [1,2) to the integer 2.... the interval [5, 6) to the integer 6. This way, each outcome will have a 1/6 chance of occurring, and remain random.

Using Math.round will rearrange which intervals are being mapped into the integers. It will also add an interval (for a total of 7 intervals). Two of these intervals [0, 0.5) & [5.5, 6.0) will be half the length of the others. This means that the integers they map to (0 & 6, respectively) will have a 1/12 probability of occurrence. Also, we don't want to map to 0, because that is not an option on a die.

It could hit 7.

(0.98 * 6 ) + 1 would hit up to 7 if you round up.

Oh, you don't need the + 1 then...

Good question, i guees it's like dominique sad.