Vely logo install | documentation | examples | changelog
16.10.0 released May 10, 2023

Purpose: Obtain a random string.

random-string to [ define ] <random string> \
    [ length <string length> ] \
    [ number | binary ]

random-string obtains a random string of length <string length>. If "length" clause is omitted, the length is 20 by default. You can create <random string> with "define" clause. <random string> is allocated memory.

If "number" clause is used, then the resulting string is composed of digits only ("0" through "9").

If "binary" clause is used, then the resulting string is binary, i.e. each byte can have an unsigned value of 0-255.

By default, if neither "number" or "binary" is used, the resulting string is alphanumeric, i.e. digits ("0" through "9") and alphabet letters ("a"-"z" and "A"-"Z") are used only.

Random generator is based on the Linux random() generator seeded by local process properties such as its PID and time. A single process is seeded once, and thus any number of requests served by the same process will use a subset of the process' random sequence. Due to joint entropy, each result given to any request is random, not just within a single request, but among any number of different requests.

Note that in any case, the random string is null-terminated.
Get a 100-digit long random value (as an alphanumeric string):
random-string to define str length 100
pf-out "%s\n", str

Get a random number of length 10 in string representation:
random-string to define str length 10 number
pf-out "%s\n", str

Get a random binary value that is 8 bytes in length - this value may contain null bytes (i.e. it will contain bytes with values ranging from 0 to 255) and will also be null-terminated regardless:
random-string to define str length 8 binary

See also
Encryption ( decrypt-data   derive-key   encrypt-data   hash-string   random-crypto   random-string  )  SEE ALL (documentation)

Copyright (c) 2017-2023 Dasoftver LLC