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.
- Best practices for unsubscribing from subscriptions of Observables in Angular
- 10 best practices for creating Angular Application
- How to GroupBy in MongoDB
- How to Join two collections in MongoDB
- How to take screenshot on Android Phone- Samsung, Redmi Note