Generate a random number between two numbers in JavaScript

Today we’ll show you how to generate a random number between two numbers in JavaScript. In the previous article we have explained about Generating an n-digit random number.

Check out more article on JavaScript.

Generate a random number between two numbers

  1. Get a random number between two numbers including min and max numbers
  2. Get a random number between 0 to n number

1. Get a random number between two numbers including min and max numbers

By the use of the below method, you will get the random number between two numbers including min and max values.

We have used the Math.random() method to generate the random values and Math.floor method to round a number downward to its nearest integer. You have to pass the min and max values in the parameter of the function to generate a random number between those values.

2. Get a random number between 0 to n number

Use the below method to get a random number between 0 to n number.

In the above method, we have used the single parameter as max value and based on it we are generating the random number between 0 to max value. We have used the same methods Math.random() and Math.floor() to generate the random number.

That’s it for today.
Thank you for reading. Happy Coding..!!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *