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

Omar Farag
Omar Farag
4,573 Points

JavaScript Random Number Generator

So I made this Random Number Generator in JS in which you can choose where your random number wants to be in-between. The thing is, it works with small numbers, but whenever I enter numbers bigger than 7, the random numbers are bigger than they are supposed to be.

Livia Dobai
Livia Dobai
21,894 Points

Can you please post your code?

3 Answers

Livia Dobai
Livia Dobai
21,894 Points

For a random number between 1 and som top number should be: var dieRoll = Math.floor(Math.random() * topNumber) +1;

and between two numbers:

var dieRoll = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;

I suggest you that instead of num1 and num2 use more descriptive names for the variables...

Omar Farag
Omar Farag
4,573 Points

Let me try that out.

Omar Farag
Omar Farag
4,573 Points

It works, but I don't understand what's happening. Would you mind explaining? Thanks.

Omar Farag
Omar Farag
4,573 Points

Here's the code:

var num1 = prompt("Enter a starting number that you wan't your random number to be between");

var num2 = prompt("Enter and end number that you wan't your random number ro be between");

num1 = parseInt(num1);
num2 = parseInt(num2);

var dieRoll = Math.ceil(Math.random() * num2) + num1;

alert("You rolled a " + dieRoll);
Livia Dobai
Livia Dobai
21,894 Points

Math.random() returns a floating point random number between 0 and 1, not including 1 If you multiply the random result with a whole number we get a value between 0 and tha whole number -1

Math.random() * 6 returns a floating pount number between 0 and 5

If we want an integer az a value we can use the Math.floor() or Math.ceil() methods

Math.floor(Math.random() * 6) returns a whole number between 0 and 5

If we want a number between 0 and 6 we can add 1 to the result

Math.floor(Math.random() * 6) + 1 returns a whole number between 0 and 6

If we want the bottom number to be greater than 1 we must use the sequent formula: multiply the random number with (topNumber - bottomNumber +1) and add the the bottomNumber to it

Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;

You can see the whole explanation in this video lesson: https://teamtreehouse.com/library/javascript-basics/working-with-numbers/the-random-challenge-solution

Hope it’s more clear now...