Skip to content

RANDSTR Expression

RANDSTR generates a cryptographically secure random string directly inside an ACE expression.

Syntax

RANDSTR{length}
RANDSTR{length;mode}

mode is optional. When omitted, ACE uses ALNUM.

The requested length must be from 1 to 1024 characters.

Modes

Mode Characters Example use
ALNUM A-Z, a-z, 0-9 General-purpose random identifiers and tokens
NUMERIC 0-9 Numeric-only codes
ALPHA A-Z, a-z Letter-only random strings
HEX 0-9, a-f Hexadecimal random values

Mode names are case-insensitive. ALNUM is the default.

Examples

RANDSTR{32}
RANDSTR{32;ALNUM}
RANDSTR{8;NUMERIC}
RANDSTR{24;ALPHA}
RANDSTR{64;HEX}

RANDSTR{32} and RANDSTR{32;ALNUM} use the same character set and generation path. As with any random generation, two calls can theoretically produce the same value; RANDSTR is not a uniqueness guarantee.

Security

The production implementation is explicit about the security properties used here:

  • it uses OpenSSL RAND_priv_bytes;
  • character selection uses rejection sampling rather than direct modulo mapping, avoiding modulo bias for the supported character-set sizes;
  • if RAND_priv_bytes reports failure, ACE throws an error instead of falling back to RANDOM, rand(), a timestamp, or another weaker generator.

RANDSTR provides cryptographic unpredictability, but random generation alone does not guarantee uniqueness. If a value must also be unique in a persistent namespace, enforce uniqueness where it is stored.

Validation

ACE rejects RANDSTR when:

  • length is missing, zero, non-numeric, or greater than 1024;
  • more than one optional parameter is supplied;
  • mode is missing after ; or is not one of ALNUM, NUMERIC, ALPHA, or HEX.

Examples of invalid expressions already covered by the RANDSTR tests include:

RANDSTR{}
RANDSTR{0}
RANDSTR{1025}
RANDSTR{16;}
RANDSTR{16;SYMBOLS}
RANDSTR{16;HEX;EXTRA}

RANDSTR vs. RANDOM

Use RANDOM{low,high} for general-purpose numeric random choices. Use RANDSTR{length[;mode]} when you need a cryptographically secure unpredictable string.