Toolbox-XToolbox-X

Generate Random Numeric

Generate a random numeric string of the specified length.

randomNumeric

The randomNumeric function generates a random numeric string of the specified length.

Info

Does not rely on Node.js or Web APIs. Works on any JS engine

Function Signature

randomNumeric(length?: number): `${number}`

Parameters

ParameterTypeDescription
lengthnumber (optional)Length of the numeric string to generate. Default: 6

Return Value

Returns a randomly generated numeric string (as a template literal type matching ${number}) of the specified length.

Example Usage

playground.ts

Use Cases

  • Generating One-Time Passwords (OTPs)
  • Creating verification codes (PINs)
  • Generating numeric transaction references
  • Creating numeric coupon or discount codes

Implementation Notes

  • If length is 0 or negative, an empty string is returned.
  • Uses crypto.getRandomValues when available for secure randomness, and falls back to Math.random if not.

See Also

  • randomHex - Generate cryptographically secure random hex strings
  • randomBytes - Generate cryptographically secure random bytes (Uint8Array)
  • randomAlphaNumeric - Generate cryptographically secure random alphanumeric strings
  • randomID - Generate customizable random alphanumeric ID (cryptographically not secure)

Last updated: Tue, Jun 16, 2026 06:17:07PM (UTC)

On this page