Suppose we need to generate a random integer or a series of randomly returned numbers. Is it possible to do so with JavaScript functionality? And how is it possible to generate a particular random sequence? Let's find out how the JavaScript Random Number function works and how you can utilize it.

Math.random() function

Using JavaScript, it's possible to make the most of Math.random() function. Despite it having slightly limited capability, this function returns a random number between 0 and 1. Why between these two? The former is regarded as the inclusive number, while the latter is exclusive. You can check it on your own by entering:

// Returns a random number:
Math.random();

As a result, you'll see a number between 0 and 1, with our input returning the following number: 0.7769876476229187. While this function certainly returns a random number, it doesn't seem that useful for a range of numbers you might have.

Math.random() function for a greater range of numbers can be used to generate a random integer within a specified range. An integer is colloquially also a number, which, however, can be written without a fractional component. Take such numbers as 8, 76, or 284 as examples.

Math.random() used along with Math.floor() can be used to return these random integers, using the following input:

// Returns a random integer from 0 to 149:
Math.floor(Math.random() * 150);

When you specify the range from 0 to 149, the Math.random() closet equals 150 to represent that all the numbers below it are included in this return. If you run this function a few times, you'll get numbers like 43, 75, and 84, never to reach 150.

Get random integer

Now, bearing the knowledge of Math.random() and Math.floor(), you might also wonder how to generate an integer between two values. For this sole purpose, you'll need to create the function like getRandomInt.

In the following example, you can see that a random integer returns between the specified min and max values. The returned integer cannot be lower than min and will always lower (and never equal to) max:

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
  }

This approach can be life-saving when you need to generate an integer between the two predetermined numbers. Just change min and max values to match your intent to generate an integer.

Security concerns

Before reviewing some more examples of how JavaScript commands and functions can be used to work with random numbers, the question of security should be highlighted. Math.random() and any other integer-related functions of JS do not provide cryptographically secure results.

In other words, numbers returned using Math.random(), Math.floor(), and our getRandomInt functions are not secure. Thus, don't rely on the returned results for any personal inquiries or projects that work with integers or numbers in any form.

As for the console.log, the numbers 4 and 8 represent the range displayed in the function cs(x,y). In other words, 4 and 8 form the range in which the cryptographically strong integers can be returned. Try experimenting with this function by replacing 4 and 8 with different numbers to see some fascinating results.

Simplifying Math.random()

The mentioned example of getRandomInt might seem a bit confusing for JavaScript beginners. That's why we're here to show how a single line of code in the form of a single function can return an integer in the simplest way.

Math.floor(Math.random() *  max);

Using this line of code, you can generate a variable between 0 and max. However, please note that this function will generate an integer from 0 (inclusive) and max (non-inclusive or exclusive). If you have an intent to return an integer from two inclusive numbers, the following function is of good use:

Math.round(Math.random() *  max);

Math.round() function, in this instance, is placed as a substitute to Math.floor() to return an integer from the inclusive number on the end scale of the return.

No repetition numbers

Sometimes, when the number range is limited to a few integers, you might want to return numbers that are unique all the time. The use cases for such a request are endless, so you should also be aware that no repetition is possible to achieve using JavaScript. Try using the following lines of code:

function generateRandomRange() {
    const max = 20;
    const random = [];
    for (var i = 0; i < max; i++) {
       const temp = Math.floor(Math.random() * max);
       if (random.indexOf(temp) == -1) {
          random.push(temp);
       } else {
          i--;
       }
    }
    console.log(random)
 }
 
 generateRandomRange();

With this request, you request a generateRandomRange function, with a maximum variable of 20. If you're attentive enough, you'll also see that Math.floor and Math.random functions are used to determine the number output.

If you run this command, you'll get a total of 20 variables, which don't repeat themselves and appear in random order all the time. If you re-run the command, you won't spot any similarities between the two returns. To increase the input size, change the max variable to greater than 20.

Wrapping up

You see, JavaScript might not be the most intuitive programming language to get the random numbers in just a single command. To apprehend this function, you have to be aware of inclusive and exclusive numbers as well as differentiate between numbers and integers.

While returning a number between 0 and 1 using Math.random() is easy, using other functions listed on our guide for more nuanced purposes can be a bit more challenging. Nevertheless, some practice with the functions above won't hurt and will certainly deepen your knowledge of random number generation using JavaScript.