These functions can be used when generating random data values.
Note
| Function | Description |
|---|---|
rand() | Generates and returns a random floating point number |
rand::bool() | Generates and returns a random boolean |
rand::duration() | Generates and returns a random duration |
rand::enum() | Randomly picks a value from the specified values |
rand::float() | Generates and returns a random floating point number |
rand::id() | Generates and returns a random id |
rand::int() | Generates and returns a random integer |
rand::string() | Generates and returns a random string |
rand::time() | Generates and returns a random datetime |
rand::uuid() | Generates and returns a random UUID |
rand::uuid::v4() | Generates and returns a random Version 4 UUID |
rand::ulid() | Generates and returns a random ULID |
rand
The rand function generates a random float, between 0 and 1.
The following example shows this function, and its output, when used in a RETURN statement:
The following example shows this function being used in a SELECT statement with an ORDER BY clause:
rand::bool
The rand::bool function generates a random boolean value.
The following example shows this function, and its output, when used in a RETURN statement:
rand::duration
The rand::duration function generates a random duration value between two duration arguments.
Some examples of the function in use:
rand::enum
The rand::enum function generates a random value, from a multitude of values.
The argument to this function can take either comma-separated values or an array of values.
As nested values are not combined at greater levels of depth, the following example will return either [8, 9] or [10, 11], but never an individual number.
rand::float
The rand::float function generates a random float, between 0 and 1.
If two numbers are provided, the function generates a random float between them (inclusive of the bounds). The first argument is the minimum and the second is the maximum.
The following example shows this function, and its output, when used in a RETURN statement:
rand::id
Note
The rand::id function generates a random alphanumeric ID, defaulting to a length of 20 characters.
If a number is provided, then the function generates a random ID with a specific length.
If a second number is provided, the function generates a random id with a length between the two numbers. The first argument is the minimum length and the second is the maximum. Both must be non-negative.
The following example shows this function, and its output, when used in a RETURN statement:
This function is used for default record ID keys in SurrealDB, and can be overridden to use a ULID or UUID instead by affixing :ulid() and :uuid() after the table name, respectively.
Output:
rand::int
The rand::int function generates a random int.
If two numbers are provided, the function generates a random int between them (inclusive). The first argument is the minimum and the second is the maximum.
The following example shows this function, and its output, when used in a RETURN statement:
rand::string
The rand::string function generates a random string, with 32 characters.
The rand::string function generates a random string, with a specific length.
If two numbers are provided, the function generates a random string with a length between the two numbers. The first argument is the minimum length and the second is the maximum. Both must be non-negative.
The following example shows this function, and its output, when used in a RETURN statement:
rand::time
The rand::time function generates a random datetime.
The rand::time function generates a random datetime, either a completely random datetime when no arguments are passed in, or between two bounds. With two arguments, the first is the earliest bound and the second is the latest (each may be a unix timestamp or a datetime).
This function can take two datetimes, returning a random datetime in between the least and greatest of the two.
Either of the arguments of this function can now be either a number or a datetime.
As of this version, this function returns a datetime between 0000-01-01T00:00:00Z and 9999-12-31T23:59:59Z. Before this, the function returned a random datetime between 1970-01-01T00:00:00Z (0 seconds after the UNIX epoch) and +262142-12-31T23:59:59Z (the maximum possible value for a datetime).
rand::uuid
The rand::uuid function generates a random Version 7 UUID.
The following example shows this function, and its output, when used in a RETURN statement:
The rand::uuid function can also generate a random UUID from a datetime.
Note that a UUID has a precision of one millisecond, and thus one converted back to a datetime will truncate nanosecond precision.
The rand::uuid function can also be called using its alias rand::uuid::v7.
rand::uuid::v4
The rand::uuid::v4 function generates a random version 4 UUID.
The following example shows this function, and its output, when used in a RETURN statement:
rand::ulid
The rand::ulid function generates a random ULID.
The following example shows this function, and its output, when used in a RETURN statement:
The rand::ulid function can also generate a random ULID from a datetime type.
The following example shows this function, and its output, when used in a RETURN statement:
Note that a ULID has a precision of one millisecond, and thus one converted back to a datetime will truncate nanosecond precision.