random integer in nodejs

// Step 1: Import the 'crypto' module for generating random bytes.
const crypto = require('crypto');

// Step 2: Define a function to generate a random integer within a specified range.
function getRandomInt(min, max) {
  // Step 3: Calculate the range and generate a random buffer of that size.
  const range = max - min + 1;
  const randomBytes = crypto.randomBytes(4);

  // Step 4: Convert the buffer to a 32-bit unsigned integer.
  const randomNumber = randomBytes.readUInt32BE(0);

  // Step 5: Map the random number to the specified range and return the result.
  return min + Math.floor(randomNumber / 0xFFFFFFFF * range);
}

// Step 6: Specify the desired range for the random integer.
const minRange = 1;
const maxRange = 100;

// Step 7: Call the function to get a random integer within the specified range.
const randomInteger = getRandomInt(minRange, maxRange);

// Step 8: Print the random integer.
console.log(randomInteger);