How to generate an 8 digits random number in Node.js

4 Answers

0 votes
const crypto = require('crypto');

for (let i = 0; i < 10; i++) {
  // crypto.randomInt(min, max) includes min and EXCLUDES max.
  // Passing 100000000 allows max result 99999999.
  const randomNumber = crypto.randomInt(10000000, 100000000);

  console.log(randomNumber);
}


/*
run:

74817153
49405523
70674348
41630256
45250364
41998516
45589636
74842348
98039674
51026695

*/

 



answered Oct 7, 2024 by avibootz
edited Oct 2 by avibootz
0 votes
function getRandom8DigitNumber() {
  const min = 10000000;
  const max = 99999999;

  return Math.floor(Math.random() * (max - min + 1)) + min;
}

for (let i = 0; i < 10; i++) {
  console.log(getRandom8DigitNumber());
}


/*
run:

88262024
43975563
30531736
65369364
89122626
86127139
13615748
36274743
79851337
35523576

*/

 



answered Oct 2 by avibootz
0 votes
const crypto = require('crypto');

function generate8DigitCode() {
  // Generates integer in range [0, 99999999]
  const rawValue = crypto.randomInt(0, 100000000);
  // Zero-pads to 8 digits (e.g., "00412983")

  return String(rawValue).padStart(8, '0');
}

for (let i = 0; i < 10; i++) {
  console.log(generate8DigitCode());
}


/*
run:

90167342
07966453
51119244
27595398
93078391
74682851
20086905
73698842
85294131
61569041

*/

 



answered Oct 2 by avibootz
0 votes
import crypto from 'node:crypto';

/**
 * Generates a random 8-digit integer between 10,000,000 and 99,999,999 (inclusive).
 * Guaranteed to start with a non-zero digit (1-9).
 *
 * @returns {number} An 8-digit positive integer.
 */
export function generate8DigitNumber() {
  const min = 10_000_000;
  const maxExclusive = 100_000_000; // crypto.randomInt(min, max) excludes the max bound

  // crypto.randomInt guarantees a uniform distribution using OS entropy
  return crypto.randomInt(min, maxExclusive);
}

/**
 * Generates an 8-character numeric string code between "00000000" and "99999999".
 * Preserves leading zeros, making it suitable for OTPs, PINs, or verification tokens.
 *
 * @returns {string} An 8-character numeric string padded with leading zeros if needed.
 */
export function generate8DigitCode() {
  // Generate a random integer in range [0, 99999999]
  const rawValue = crypto.randomInt(0, 100_000_000);

  // Pad short integers with leading zeros to guarantee exact 8-character length
  return String(rawValue).padStart(8, '0');
}

// ==========================================
// Example Execution & Usage
// ==========================================

function main() {
  try {
    // 1. Generate an 8-digit mathematical integer [10000000 to 99999999]
    const randomNumber = generate8DigitNumber();
    console.log(`Generated 8-digit integer: ${randomNumber}`);

    // 2. Generate an 8-character string code [00000000 to 99999999] preserving leading zeros
    const randomCode = generate8DigitCode();
    console.log(`Generated 8-digit OTP/PIN code: ${randomCode}`);
  } catch (error) {
    console.error('Failed to generate random value:', error);
  }
}

// Run the script directly if executed as the main module
main();


/*
run:

Generated 8-digit integer: 43933748
Generated 8-digit OTP/PIN code: 00007931

*/

 



answered Oct 2 by avibootz
...