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) Creating Reusable Code with Functions Random Number Challenge

arik
arik
5,791 Points

Generating random number between x and y or generating random number from x to y ?

Mr Dave said to generate random number between x and y, but why the solution include x and y? If it were between shouldn't it excludes x and y, or in other words :

x < random number < y

So the formula should be this..

Math.floor ( Math.random * ( y - x - 1 ) )+ ( x + 1 );

if He uses the formula written in the random.js workspace, it means generating random number from x to y or in other words, x <=random number <= y

Please correct me if I am wrong....

2 Answers

Steven Parker
Steven Parker
230,230 Points

Programming terms sometimes vary from mathematical ones.

From a mathematical perspective you're totally right, the word "between" would mean exclusive of the end points. But in programming "between" can be either exclusive or inclusive; and when not explicitly specified, it's most likely to mean inclusive. In SQL it is an operator keyword, and it is always inclusive.

In the case of the video, the formula shown is inclusive just as you described.

If you think "between" is confusing, in programming a "variable" can be a "constant".

arik
arik
5,791 Points

Thank you very much indeed for the answer. I get the point now. If it's not specified it means inclusive.