Create a Random Number Generator with JavaScript

Often, there are times where your application needs to have a random number generated. The reason will be specific to the application you're developing, but it could be you're creating a game of chance, including a dice or playing card game. Or you want to pick a random profile from your site to perform an action or show up in a listing.

This article will go over a few of the JavaScript Math object functions and how they can work within your application for random number generating.

The Math Object Functions We'll Be Exploring

Here are a few of the Math object functions that we'll be exploring in our example.

  • Math.random(): Returns a pseudo-random, floating-point number in a range between 0 and 1.
  • Math.floor(): Rounds down to the nearest integer.
  • Math.ceil(): Rounds up to the nearest integer.
JavaScript comes with these Math object functions built-in, so you don't have to worry about browser compatibility on either desktop, tablet, or mobile browsers. The only time where JavaScript would not work within a web browser is if the user has disabled it in their browser settings. There is nothing you can do about this but, since the settings comes enabled by default, you should rarely run into this issue.

The Math.random() Method

JavaScript's Math.random() method returns a pseudo-random, floating-point number in a range between 0 and 1. This means that you'll end up setting random numbers generated like this:

console.log(Math.random());
// 0.8916108284965996

Chances are, this output will not be useful to you or your application, so let's look into how to put this in play.

Generate a Random Number Within a Specified Range

Probably one of the most used scenarios for random number generation is retrieving an integer within a specified range defined by the user.

Let's explore a simple function that can achieve this:

function random_num(min, max) {
return Math.floor(Math.random() * ((max - min) + 1) + min);
}

var num = random_num(1, 10);

console.log(num);
// returns an integer 1 - 10

In this example, we're returning an integer between 1 and 10. You can change these parameter values to anything you like to test the results or change them to fit within your application's rules.

The function random_num() accepts two parameters, min and max, which are both inclusive.

When I say inclusive, I mean both the parameter's values can be returned as possible results from the function call. If we removed the +1 from the example above, the max value would be exclusive, meaning the value that we pass in for that parameter could never be returned by the function.

Conclusion

In this article, you learned how some of the Math object functions work within JavaScript, including how to generate a random floating-point number and converting it into a useful integer between a certain range.

Since JavaScript comes with these functions already built-in, you don't need to worry about compatibility issues within any web browser.

Published: December 13, 2020