SurrealDB Docs Logo

Enter a search query

Rand functions

These functions can be used when generating random data values.

FunctionDescription
rand()Generates and returns a random floating point number
rand::bool()Generates and returns a random boolean
rand::enum()Randomly picks a value from the specified values
rand::float()Generates and returns a random floating point number
rand::guid()Generates and returns a random guid
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::uuid::v7()Generates and returns a random Version 7 UUID
rand::ulid()Generates and returns a random ULID

rand

The rand function generates a random float, between 0 and 1.

API DEFINITION
rand() -> number

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand(); 0.7062321084863658

The following example shows this function being used in a SELECT statement with an ORDER BY clause:

SELECT * FROM [{ age: 33 }, { age: 45 }, { age: 39 }] ORDER BY rand(); [ { age: 45 }, { age: 39 }, { age: 33 } ]

rand::bool

The rand::bool function generates a random boolean value.

API DEFINITION
rand::bool() -> bool

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::bool(); true

rand::enum

The rand::enum function generates a random value, from a multitude of values.

API DEFINITION
rand::enum(value...) -> any

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::enum('one', 'two', 3, 4.15385, 'five', true); "five"

rand::float

The rand::float function generates a random float, between 0 and 1.

API DEFINITION
rand::float() -> float

If two numbers are provided, then the function generates a random float, between two numbers.

API DEFINITION
rand::float(number, number) -> float

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::float(); 0.7812733136200293
RETURN rand::float(10, 15); 11.305355983514927

rand::guid

The rand::guid function generates a 20-characters random guid.

API DEFINITION
rand::guid() -> string

If a number is provided, then the function generates a random guid, with a specific length.

API DEFINITION
rand::guid(number) -> string

If a second number is provided, then the function will generate a random guid, with a length between the two numbers.

API DEFINITION
rand::guid(min, max) -> string

The following example shows this function, and its output, when used in a RETURN statement:

A 20-chars random guid
RETURN rand::guid(); "4uqmrmtjhtjeg77et0dl"
A 10-chars random guid
RETURN rand::guid(10); "f3b6cjh0nt"
A random guid with a length between 5 and 15 chars
RETURN rand::guid(5, 15); "894bqt4lp"

rand::int

The rand::int function generates a random int.

API DEFINITION
rand::int() -> int

If two numbers are provided, then the function generates a random int, between two numbers.

API DEFINITION
rand::int(number, number) -> int

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::int(); 6841551695902514727
RETURN rand::int(10, 15); 13

rand::string

The rand::string function generates a random string, with 32 characters.

API DEFINITION
rand::string() -> string

The rand::string function generates a random string, with a specific length.

API DEFINITION
rand::string(number) -> string

If two numbers are provided, then the function generates a random string, with a length between two numbers.

API DEFINITION
rand::string(number, number) -> string

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::string(); "N8Q86mklN6U7kv0A2XCRh5UlpQMSvdoT"
RETURN rand::string(15); "aSCtrfJj4pSJ7Xq"
RETURN rand::string(10, 15); "rEUWFUMcx0YH"

rand::time

The rand::time function generates a random datetime.

API DEFINITION
rand::time() -> datetime

The rand::time function generates a random datetime, between two unix timestamps.

API DEFINITION
rand::time(number, number) -> datetime

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::time(); d"2026-09-07T04:27:53Z"
RETURN rand::time(198371, 1223138713); d"1991-01-13T23:27:17Z"

rand::uuid

The rand::uuid function generates a random UUID. You can also generate uuids from datatime values.

API DEFINITION
rand::uuid() -> uuid

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::uuid(); [u"e20b2836-e689-4643-998d-b17a16800323"]

rand::uuid from timestamp

Available since: v2.0.0

The rand::uuid function generates a random UUID from a datetime type.

API DEFINITION
rand::uuid(datetime) -> uuid

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::uuid(d"2021-09-07T04:27:53Z");
CREATE ONLY test:[rand::uuid()] SET created = time::now(), num = 1; SLEEP 100ms; LET $rec = CREATE ONLY test:[rand::uuid()] SET created = time::now(), num = 2; SLEEP 100ms; CREATE ONLY test:[rand::uuid()] SET created = time::now(), num = 3; -- Select the value of the record created before the current record in the table SELECT VALUE num FROM test:[rand::uuid($rec.created - 100ms)]..;

rand::uuid::v4

The rand::uuid::v4 function generates a random version 4 UUID.

API DEFINITION
rand::uuid::v4() -> uuid

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::uuid::v4(); [u"4def23a5-a847-4934-8dad-c64ccc48921b"]

rand::uuid::v4 from timestamp

Available since: v2.0.0

The rand::uuid::v4 function generates a random version 4 UUID from a datetime type.

API DEFINITION
rand::uuid::v4(datetime) -> uuid

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::uuid::v4(d"2021-09-07T04:27:53Z");
CREATE ONLY test:[rand::uuid::v4()] SET created = time::now(), num = 1; SLEEP 100ms; LET $rec = CREATE ONLY test:[rand::uuid::v4()] SET created = time::now(), num = 2; SLEEP 100ms; CREATE ONLY test:[rand::uuid::v4()] SET created = time::now(), num = 3; -- Select the value of the record created before the current record in the table SELECT VALUE num FROM test:[rand::uuid::v4($rec.created - 100ms)]..;

rand::uuid::v7

The rand::uuid::v7 function generates a random Version 7 UUID.

API DEFINITION
rand::uuid::v7() -> uuid

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::uuid::v7(); [u'0190d9df-c6cd-7e8a-aae2-aa3a162507ed']

rand::uuid::v7 from timestamp

Available since: v2.0.0

The rand::uuid::v7 function generates a random Version 7 UUID from a datetime type.

API DEFINITION
rand::uuid::v7(datetime) -> uuid

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::uuid::v7(d"2021-09-07T04:27:53Z");
CREATE ONLY test:[rand::uuid::v7()] SET created = time::now(), num = 1; SLEEP 100ms; LET $rec = CREATE ONLY test:[rand::uuid::v7()] SET created = time::now(), num = 2; SLEEP 100ms; CREATE ONLY test:[rand::uuid::v7()] SET created = time::now(), num = 3; -- Select the value of the record created before the current record in the table SELECT VALUE num FROM test:[rand::uuid::v7($rec.created - 100ms)]..;

To enable rand::uuid::v7 in embedded mode you need to add the following to your .cargo/config.toml file in your project

[build]
rustflags = ["--cfg", "uuid_unstable"]

rand::ulid

The rand::ulid function generates a random ULID.

API DEFINITION
rand::ulid() -> ulid

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::ulid(); [u"01H9QDG81Q7SB33RXB7BEZBK7G"]

rand::ulid from timestamp

Available since: v2.0.0

The rand::ulid function generates a random ULID from a datetime type.

API DEFINITION
rand::ulid(datetime) -> uuid

The following example shows this function, and its output, when used in a RETURN statement:

RETURN rand::ulid(d"2021-09-07T04:27:53Z");
CREATE ONLY test:[rand::ulid()] SET created = time::now(), num = 1; SLEEP 100ms; LET $rec = CREATE ONLY test:[rand::ulid()] SET created = time::now(), num = 2; SLEEP 100ms; CREATE ONLY test:[rand::ulid()] SET created = time::now(), num = 3; -- Select the value of the record created before the current record in the table SELECT VALUE num FROM test:[rand::ulid($rec.created - 100ms)]..;


© SurrealDB GitHub Discord Community Cloud Features Releases Install