Hi everyone! Welcome to another edition of Boring JavaScript.

This time we tackle how to generate random numbers in JavaScript with the Math.random() method.

## Random Actions

Generating a random number in JavaScript is very easy. All it takes is one method:

```
const number = Math.random();
console.log(`My number is ${number}`);
```

Simple, right?

The Math.random() method (it takes no arguments) generates a random number for from 0 to 1, exclusive of 1. Meaning – the number will be from 0 to .99999999999999999. This is important later when you wish to use this random number for real-world purposes.

Running the above code will give you something like this:

`thevirtuoid@boringjavascript:~$: node random`

My number is 0.017507536904644905

Each time you call Math.random(), the number will be different. Just realize that it will always be between 0 and .99999999999999999.

## Give Me a Number between 1 and 10

Normally, a number between 0 and 1 (excluding 1) is not used in normal interactions, either with apps or with people. For example, you’ll never go up to someone and say “Give me a number between 0 and 1, exclusive of 1”. Nope. Normally, you will say something like “Give me a number between 1 and 10”. And the response isn’t going to be something like “2.3628841067993” – it will be an integer number between 1 and 10, inculsive.

Therefore, what do we need to do to get that type of range?

Let’s say we want the numbers between 1 and 10. Let’s take a look:

```
const countOfNumbers = 10;
const number = Math.random * countOfNumbers;
console.log(`Original Number: ${number}`);
const finalNumber = Math.ceil(number);
console.log(`A number between 1 and ${countOfNumbers}: ${finalNumber}`);
```

There are two important pieces here:

First is the ‘countOfNumbers’ variable. This is the number of ‘numbers’ within the range you want. In our instance, we have 10 numbers, as we want the numbers between 1 and 10, inclusive. If we want 1 to 100, then we would want 100 numbers. 5 through 37? Then that would be 32 numbers. You get the idea. Once we have our ‘countOfNumbers’, we then multiply our Math.random() number by that variable to get a value between 0 and the ‘countOfNumbers’, exclusive of ‘countOfNumbers’. Why? Remember that Math.random() generates a number between 0 and 1, exclusive of 1. Since ‘countOfNumbers’ times 1 is ‘countOfNumbers’, and we will never get 1, we can be certain that we will never get ‘countOfNumbers’. In our case, we’ll get a number between 0 and 9.9999999999999.

Second, we perform a Math.ceil() method on the randomized number (line 4 above). And why that? As mentioned in the last paragraph, our random number is between 0 and 9.999999999999. We don’t want the decimal portion, and we don’t want to start at 0. Math.ceil() will strip off the decimal portion of the number, then add 1 to the final number, giving us a nice, clean integer number from 1 to 10.

By the way, if you wanted 0 to 9, use Math.floor().

So what about one of my examples above that is weird, like ‘Give me a number between 5 and 37’? Simple! Here’s the code:

```
const startNumber = 5;
const endNumber = 37;
const totalNumbers = endNumber - startNumber + 1;
const rawRandomNumber = Math.random() * totalNumbers;
const rawIntegerNumber = Math.floor(rawRandomNumber);
const finalNumber = rawIntergerNumber + startNumber;
console.log(`A number between ${startNumber} and ${endNumber}: ${finalNumber}`);
```

Yep, very verbose, but easy to understand. We get the total number of ‘numbers’ (line 3), get our random number (line 4), and make it into an integer (line 5). This will give us a number between 0 and ‘totalNumbers’, exclusive of ‘totalNumbers’. Since our starting number must be 5, we add that back into the integer number to get our final range and our final number (line 6).

Now you can get a random number from any range you choose!

## Show me the Video!

In our video for this edition, we also include a Real Life Application of using Math.random() – specifcially, using Math.random() at your next D&D game. Check it out!

And make sure to subscribe to our YouTube channel!

## Conclusion

You didn’t know how easy it was to generate random numbers, did you? Now you know how to generate random numbers, how to generate based upon any range, and if you watched the video, how to be the coolest kid on the block in your next D&D game.

Or bore everyone to death at your next game.

Thanks for reading, and we’ll see you next time!

Categories: Boring JavaScript Javascript

### thevirtuoid

Web Tinkerer. No, not like Tinkerbell.

Creator of the game Virtuoid. Boring JavaScript. Visit us at thevirtuoid.com

## Leave a Reply