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
ab199
Full Stack JavaScript Techdegree Student 18,825 PointsProgramming problems: Math and logic. Where can we better learn these skills?
I had problems figuring out the solution to "Random Number Challenge Solution" in the JavaScript Basics course. This was the final challenge in the course. Dave McFarland said that it should be pretty easy since he gave us the formula and all we had to do was plugin the numbers. I couldn't figure it out. I did get really close though. I hope that I'm not alone here. I was wondering if anyone had any suggestions about where we could solve more of these problems to understand them. Thanks for all the help!
Here's my code if you're interested:
function randomNumber(lowNumber, highNumber) {
var result = Math.floor(Math.random() * (highNumber - lowNumber + 1)) + lowNumber;
return console.log(result);
}
console.log(randomNumber(12,50));
The problem that I had was with the formula. I don't really understand the formula. It'll evaluate values between two given numbers, but I don't think it'll ever generate either of the two given numbers. For example, we use 12 and 60 as our numbers. The program will generate a random value between 12 and 60, but never 12 or 60. Therefore, the solution is never truly random, or am I wrong???
1 Answer
sean murphy
9,435 PointsTo determine a random number within a range, including the range (let's say 5 and 10), we need to first see how many different numbers there are. We do that by finding the difference (10 minus 5 is 5) and adding one (which brings us to 6), since we are including the bottom of the range. So if we roll a die with a 5, 6, 7, 8, 9, and 10 on each of its sides, the die would have 6 sides.
numOfPossibleValues = (highNumber - lowNumber + 1)
We run Math.random, which will return any number between 0 and 1. This number can be 0, but it will never be 1. Multiplying it by numOfPossibleValues will return a random number between 0 and numOfPossibleValues (including 0, but not including the numOfPossibleValues). Using Math.floor, it will round this new number down, getting rid of all those digits after the decimal. So our total possible numbers can be any whole number between
Math.floor(0 * numOfPossibleValues) === 0
all the way up to
Math.floor(.999 * numOfPossibleValues) === numOfPossibleValues - 1
Why is our highest number one less than the total number of possible values? Because we are including 0 in this multiplication. If we plug 10 and 5 into this, we'll see that we can generate any of these numbers; 0, 1, 2, 3, 4, or 5. With these numbers, all we have to do is add the lowNumber from our function to get anything from 5 (5 + 0) to 10 (5 + 5).
Math.floor(Math.random() * numOfPossibleValues) + lowNumber;
So that extra "1" we added at the start, in a way, we are removing with Math.floor.
As a side note, computers use a predetermined method to generate random numbers, so nothing is ever truly random. However, if you run this program enough times, you will find a near even distribution between, and including, both the high and the low values.