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!
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
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?
Steven Parker228,001 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.
|random() + 10||10...11||10|
|random() * 10||0...10||0...9|