generate a random number between 10000 and 99999 in javascript

index.tsx
// function to generate a random number between a range
function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// generate random number between 10000 and 99999
const randomNumber = getRandomNumber(10000, 99999);
console.log(randomNumber);
285 chars
9 lines

Explanation:

The getRandomNumber function takes two parameters min and max which represent the range of the numbers we want to generate. The Math.random() function generates a random decimal value between 0 and 1. We can multiply that by the difference between the max and min values and add 1 to include the maximum value in the range. Then we use Math.floor() to round down to the nearest integer.

We call the getRandomNumber function with the arguments 10000 and 99999 to generate a random number between 10000 and 99999. The randomNumber variable stores the random number generated, and we log it to the console.

gistlibby LogSnag