Returns a string with random characters.

The default characters to generate a string are URL-safe and contain the characters 0-9, A-Z, a-z, - and _.

Instead of the default character set, it is possible to use your own set. This custom set may contain duplicated characters which increase the chance for certain characters to occur in the final string. For example, a random string based on the character set AAB will most likely contain more A’s than B’s.

This function is using the SYS_getrandom system call to generate random data and is therefore more secure compared to the other random functions in ThingsDB.

This function does not generate a change.


randstr(length, [character-set])


Argument Type Description
length int (required) Length of the output string.
character-set str (optional) Custom character set, used to generate the output string.

Return value

A random string of a given length.


Some examples on how randstr(..) can be used:

// Returns a string of 16 random characters using the default character set.

Example return value in JSON format:

// Returns a string of 10 random characters using only HEX characters
randstr(10, '0123456789ABCDEF');

Example return value in JSON format: