textConvert
    Preparing search index...

    Function randomString

    • Generates a random string of a given length, drawn from a cryptographically secure source (globalThis.crypto.getRandomValues, the standard Web Crypto API available in both Node and browsers -- nothing here falls back to Math.random). Suitable for one-off IDs, tokens, or test fixtures.

      Parameters

      • length: number

        Number of characters to generate. Returns '' for a non-positive or non-integer length.

      • options: { charset?: string } = {}
        • Optionalcharset?: string

          'alpha' (letters only), 'numeric' (digits only), 'alphanumeric' (the default), or any other string, which is used literally as the pool of characters to draw from (e.g. 'ABC123').

      Returns string

      A random string of the requested length, or '' if length or the resolved charset is empty.

      randomString(8); // e.g. 'aZ3kD9pQ' (alphanumeric by default)
      randomString(6, { charset: 'numeric' }); // e.g. '482913'
      randomString(4, { charset: 'ABC123' }); // custom charset, e.g. 'A1C3'