javascript

Random numbers between two values in JavaScript!

Before discussing the main topic, let us see how to print a random number in plain JavaScript. If you don’t know already there is this Math.random() function available in JavaScript to generate random numbers. But, there is a problem it will not return a whole number instead it will return a float number. So first, let’s write a simple snippet to print a whole random number.

1. JavaScript snippet to print a random number:

The above snippet will print a random number in the JavaScript console.

Now, we know how to print a random number, but what actually we need is, we have to print a random number between two values, so let’s write another example:

2. JavaScript to print a random number between two given values:

This script above one will generate a random number between 10 and 15. So that’s it, you know now!.

To explore this more, I am giving another working demo & example script of both the above scenarios.

Here is the entire script for you to explore:

You can also see the demo here: 

demo

Related Posts

Leave a Reply