Random Numbers in JavaScript: Definition, Implementation & Examples

Instructor: Amit Agrawal

Amit has over 11 years of industry experience in the IT Software Domain and has a Masters Degree in Computer Applications.

In this lesson, you will learn how to generate random numbers in JavaScript. You will learn why random is not always random in computer programming and see many relevant examples.

Random Numbers in Computers

It may come as a surprise but computers cannot generate a truly random number! This is because computers are, by their very design, deterministic machines. What this means is that they perform whatever operation you give them exactly as told. So, if told to add two numbers, the result will be exactly that, not a bit more, not a bit less. This is certainly what we expect them to do in the first place. Unfortunately, this means that generating truly random numbers is impossible.

For a number to be random, there has to be absolutely no way to predict it. Since this is impossible using computers, random numbers are generated using a statistical algorithm that takes an initial input number, known as a seed number. Based on this seed number, it generates numbers that seem random and unpredictable using mathematical formulas. Such algorithms are known as Pseudo-Random Number Generators (PRNG). Good algorithms generate numbers that approximate the properties of random numbers. For most purposes, random numbers generated in this fashion are quite acceptable and all the software that you use on a daily basis that needs random numbers internally uses them perfectly well.

Random numbers generated using PRNGs are based on a starting seed value. Thus, say you use a seed value of 87, the first ten random numbers that come out of the generator could be 5, 23, 89, 32, 47, 51, 9, 76, 64 and 13. Similarly, say you use another seed value of 34, the first ten random numbers out of the generator could be 45, 22, 63, 75, 12, 87, 54, 27, 98 and 23. Of course, we have taken just the first ten random numbers using both these seed values and both the sequences don't repeat. In fact, the algorithms are designed very carefully to produce extremely large sequences without repetitions, which is perfectly fine for most applications.

As you saw above, both sequences seem random but since they follow a precise mathematical algorithm to generate them, if you use the same seed number, they will produce exactly the same sequence of random numbers each time! Internally, the mathematical algorithms take the seed value, run it through the formulas, and generate an output number that is used as the first ''random'' number, which is then used to generate the next ''random'' number, and so on. They generate the exact same sequence each time for a given seed value. So, how do you choose a random seed in the first place? Programmers faced with this dilemma try to get something from the environment—the time of the day, or the wait time between two keypresses—you get the idea! Of course, even these are not truly random since you could have a program that is scheduled to run at exactly the same time every day, but we have to start somewhere! It is extremely difficult to get genuinely random numbers in the world of computers.

Random Numbers Using JavaScript

JavaScript has a built-in function that generates pseudo-random numbers, Math.random(), which returns a floating-point number between 0 and 1. To be precise, it can return a 0, but never 1. You can then use this to get a range of numbers of your choice. The initial seed value to the pseudo-random number algorithm that this function uses is chosen by JavaScript and cannot be provided by the user.

Here are some numbers generated by calls to Math.random():









Since all of the numbers generated are between 0 (inclusive) and 1 (not inclusive), we need a way to convert them into integers, or to fall within a range that we need for the program we are writing. For example, a roll of dice should be a perfect integer within a range of 1 and 6. As another example, for GPS latitudes, our need could be between 12.55555 degrees to 27.43505 degrees.

Integers Within a Range

Let's create a function that takes two arguments, the minimum and the maximum (both inclusive) and spits out a random integer between the two.

function randInt(min, max) {

    return Math.floor(Math.random() * (max - min + 1)) + min;


Let's assume the user enters integer numbers for the two inputs and we want to return a random integer within that range. We know that the random function returns a decimal value between 0 (inclusive) and 1 (not inclusive). If we assume it returns 0, then we want to return the min value and similarly, if it returns 0.9999999 i.e. close to 1, then we want to return the max value. For any other value that the random() function returns, we want it to pick a number between this range. This is exactly what the Math.floor(Math.random() * (max - min + 1)) + min does. Here's some sample output:







To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account