This is the ThingsDB documentation for version v0, click here for the latest version!

randstr

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 an event.

Function

randstr(length, [character-set])

Arguments

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.

Example

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

// Returns a string of 16 random characters using the default character set.
randstr(16);

Example return value in JSON format:

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

Example return value in JSON format:

"7AFBAE572B"