Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Omar Farag
Omar Farag
4,571 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,571 Points

Let me try that out.

Omar Farag
Omar Farag
4,571 Points

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

Omar Farag
Omar Farag
4,571 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...