Generate N digit random number in Javascript

N digit Random Number

Random Number of N digit Javascript / N digit Random Number Javascript.

Generate N digit random number : At times you might have a requirement to generate a random number of N digit. Requirement could be to generate a random employee ID of 10 digit, a random product id of 5 digit or a category id for of 9 digit.

Below we will add a code snippet which will help you to achieve all the requirements mentioned above.

Math.floor(Math.random() * 8999999999 + 1000000000)

This will help you to generate a 10 digit random number without a zero in the initial. this number generated can be used for any of the below mentioned scenerio.

1: You have a requirement to generate a random 10 digit user ID of the users in the system. We will use random number because we don’t want to give our users an indicator of how many users we have in our system. Going with a sequential ID will give an indicator to users about number of users in the system.

2: A product ID or code in your system, which uniquely identifies something.

3: Category ID of a product.

So depending upon your requirement you can use code mentioned above to generate a random number of N digit.

Checkout our latest posts.

Happy Coding!

Generate N digit random number in Javascript

Leave a Reply

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

Scroll to top