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 The Random Challenge Solution

What do we use multiply on Math.random to the topNumber, instead of another mathematical operator?

What do we multiply instead of adding the Math.random to the topNumber? I added a plus to the code and it just added one to whatever I entered. Why does multiply randomize it?

1 Answer

Steven Parker
Steven Parker
231,271 Points

The random function always gives you a fractional value between 0 and 1. If you take the "floor", you will always get 0. And if you add something to it, the "floor" will always give you (just) the number you added.

On the other hand, if you multiply it by something, it will then be a number between 0 and whatever you multiplied it by. Then "floor" will just remove any fractional part left after the multiplication, but the range will still be extended.

Examples:

calculation value after "floor"
random() 0...1 0
random() + 10 10...11 10
random() * 10 0...10 0...9

Steven that makes a whole lot of sense how you explained that. Thank you