JavaScript: Generate Random Number in Range - Stack Abuse

JavaScript: Generate Random Number in Range


We as software developers often stumble into situations where we need to insert a dose of randomness into our code.

In this article, we will look at how to generate random numbers in JavaScript. We will also touch upon a few built-in methods to deal with random numbers. By the end, we will put this knowledge to good use by writing a function to simulate a six-sided die.

Generating Random Numbers in JavaScript

Math.random() in JavaScript generates a floating-point (decimal) random number between 0 and 1 (inclusive of 0, but not 1). Let's check this out by calling:


This will output a floating-point number similar to:


This is useful if you're dealing with percentages, as any value between 0 and 1, rounded to two decimal places, can be thought of as a percentile.

Generating Random Whole Numbers in Range

We generally don't deal with floating-point numbers in the 0 to 1 range, though. So, let's look at a way to round floating-point numbers.

We can round down a floating-point number using Math.floor(). Similarily, we can round up a number via the Math.ceil() function:


This will give us the output:


Let's generate a random number between min and max, not including max:

function randomNumber(min, max){
    const r = Math.random()*(max-min) + min
    return Math.floor(r)

Alternatively, we could've included max with the Math.ceil() function instead.

We are multiplying with (max-min) to transform the range [0,1) into [0, max-min). Next, to get a random number in the required range, we are adding min. Finally, we are rounding this to an integer using Math.floor().

Let's call this method a few times and observe the results:

for (var i = 0; i < 10; i++) {
  console.log(randomNumber(0, 10))

This will output something similar to:



Generating pseudo-random numbers in a program can be used to simulate unpredictability of an enemy in-game, or for randomization of forests in a block-like game we all know and love. It can also be used to simulate random inputs while testing another program you wrote.

Either way, generating a random number is an important tool in any engineer's toolkit, and should be expanded as much as possible with different generation methods and algorithms. This article was just the first step of learning random number generation.

Last Updated: November 11th, 2020

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Abhilash KakumanuAuthor

Hey, I am a full-stack web developer located in India. I am a curious person who is always trying to wrap my head around new technologies. In my free time, I read novels and play with my dog!

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:

    © 2013-2021 Stack Abuse. All rights reserved.