These functions can be used when generating random data values.
| 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::uuid::v7() | Generates and returns a random Version 7 UUID |
rand::ulid() | Generates and returns a random ULID |
randThe rand function generates a random float, between 0 and 1.
API DEFINITIONrand() -> 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::boolThe rand::bool function generates a random boolean value.
API DEFINITIONrand::bool() -> bool
The following example shows this function, and its output, when used in a RETURN statement:
RETURN rand::bool(); true
rand::durationAvailable since: v2.3.0
The rand::duration function generates a random duration value between two duration arguments.
API DEFINITIONrand::bool(duration, duration) -> duration
Some examples of the function in use:
rand::duration(1ns, 1ms); rand::duration(0ns, duration::max);
Output-------- Query 1 -------- 435µs884ns -------- Query 2 -------- 405337457164y36w2d5h54m8s16ms76µs191ns
rand::enumThe rand::enum function generates a random value, from a multitude of values.
API DEFINITIONrand::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::floatThe rand::float function generates a random float, between 0 and 1.
API DEFINITIONrand::float() -> float
If two numbers are provided, then the function generates a random float, between two numbers.
API DEFINITIONrand::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::idNoteThis function was known as
rand::guidin versions of SurrrealDB before 3.0.0-alpha.11. The behaviour has not changed.
The rand::id function generates a random alphanumeric ID, defaulting to a length of 20 characters.
API DEFINITIONrand::id() -> string
If a number is provided, then the function generates a random ID with a specific length.
API DEFINITIONrand::id(number) -> string
If a second number is provided, then the function will generate a random id, with a length between the two numbers.
API DEFINITIONrand::id(min, max) -> string
The following example shows this function, and its output, when used in a RETURN statement:
Default 20-char random idRETURN rand::id(); '4uqmrmtjhtjeg77et0dl'
A 10-char random idRETURN rand::id(10); 'f3b6cjh0nt'
A random id with a length between 1 and 9 charsRETURN rand::id(1, 9); '894bqt4lp'
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.
CREATE person, person:ulid(), person:uuid() -- Return only id values for nicer output RETURN VALUE id;
Output:
[ person:o9s1sl3ivckuxo0kglix, person:01K7JRP6KVAQGN2THR2T13X9WP, person:u'0199e58b-1a7b-7880-ad5b-01671678c11f' ]
rand::intThe rand::int function generates a random int.
API DEFINITIONrand::int() -> int
If two numbers are provided, then the function generates a random int, between two numbers.
API DEFINITIONrand::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::stringThe rand::string function generates a random string, with 32 characters.
API DEFINITIONrand::string() -> string
The rand::string function generates a random string, with a specific length.
API DEFINITIONrand::string(number) -> string
If two numbers are provided, then the function generates a random string, with a length between two numbers.
API DEFINITIONrand::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::timeThe rand::time function generates a random datetime.
API DEFINITIONrand::time() -> datetime rand::time(datetime|number, datetime|number) -> datetime
The rand::time function generates a random datetime, either a completely random datetime when no arguments are passed in, or between two unix timestamps.
RETURN rand::time(); -- d'1327-07-12T01:00:32Z' RETURN rand::time(198371, 1223138713); -- d'1991-01-13T23:27:17Z'
Available since: v2.2.0
This function can take two datetimes, returning a random datetime in between the least and greatest of the two.
RETURN rand::time(d'1970-01-01', d'2000-01-01'); -- d'1999-05-29T17:02:16Z"
Available since: v2.3.0
Either of the arguments of this function can now be either a number or a datetime.
RETURN rand::time(0, d'1990-01-01'); -- d'1986-11-17T15:06:01Z'
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::uuidThe rand::uuid function generates a random UUID. You can also generate uuids from datetime values.
API DEFINITIONrand::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 timestampAvailable since: v2.0.0
The rand::uuid function generates a random UUID from a datetime type.
API DEFINITIONrand::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::v4The rand::uuid::v4 function generates a random version 4 UUID.
API DEFINITIONrand::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 timestampAvailable since: v2.0.0
The rand::uuid::v4 function generates a random version 4 UUID from a datetime type.
API DEFINITIONrand::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::v7The rand::uuid::v7 function generates a random Version 7 UUID.
API DEFINITIONrand::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 timestampAvailable since: v2.0.0
The rand::uuid::v7 function generates a random Version 7 UUID from a datetime type.
API DEFINITIONrand::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::ulidThe rand::ulid function generates a random ULID.
API DEFINITIONrand::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 timestampAvailable since: v2.0.0
The rand::ulid function generates a random ULID from a datetime type.
API DEFINITIONrand::ulid(datetime) -> ulid
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)]..;