Random value generator
The following functions have been created to help with our test suite. They are also useful for users testing QuestDB on specific workloads in order to quickly generate large test datasets that mimic the structure of their actual data.
Values can be generated either:
- Pseudo randomly
- Deterministically
when specifying a
seed
QuestDB supports the following random generation functions:
- rnd_boolean
- rnd_byte
- rnd_short
- rnd_int
- rnd_long
- rnd_long256
- rnd_float
- rnd_double
- rnd_date
- rnd_timestamp
- rnd_char
- rnd_symbol
- rnd_str
- rnd_bin
#
UsageRandom functions should be used for populating test tables only. They do not hold values in memory and calculations should not be performed at the same time as the random numbers are generated.
For example, running
SELECT round(a,2), a FROM (SELECT rnd_double() a FROM long_sequence(10));
is
bad practice and will return inconsistent results.
A better approach would be to populate a table and then run the query. So for example
- create -
CREATE TABLE test(val double);
- populate -
INSERT INTO test SELECT * FROM (SELECT rnd_double() FROM long_sequence(10));
- query -
SELECT round(val,2) FROM test;
#
Generating sequencesThis page describes the functions to generate values. To generate sequences of values, please refer the page about row generators.
#
rnd_booleanrnd_boolean()
- generates a random boolean
value, either true
or false
,
both having equal probability.
Return value:
Return value type is boolean
.
Examples:
a | b |
---|---|
true | 47 |
false | 53 |
#
rnd_byternd_byte()
- returns a random integer which can take any value between0
and127
.rnd_byte(min, max)
- generates byte values in a specific range (for example only positive, or between 1 and 10).
Arguments:
min
: is abyte
representing the lowest possible generated value (inclusive).max
: is abyte
representing the highest possible generated value (inclusive).
Return value:
Return value type is byte
.
Examples:
#
rnd_shortrnd_short()
- returns a random integer which can take any value between-32768
and32767
.rnd_short(min, max)
- returns short values in a specific range (for example only positive, or between 1 and 10). Supplyingmin
abovemax
will result in aninvalid range
error.
Arguments:
min
: is ashort
representing the lowest possible generated value (inclusive).max
: is ashort
representing the highest possible generated value (inclusive).
Return value:
Return value type is short
.
Examples:
#
rnd_intrnd_int()
is used to return a random integer which can take any value between-2147483648
and2147483647
.rnd_int(min, max, nanRate)
is used to generate int values in a specific range (for example only positive, or between 1 and 10), or to get occasionalNaN
values along with int values.
Arguments:
min
: is anint
representing the lowest possible generated value (inclusive).max
: is anint
representing the highest possible generated value (inclusive).nanRate
is anint
defining the frequency of occurrence ofNaN
values:0
: NoNaN
will be returned.1
: Will only returnNaN
.N > 1
: On average, one in N generated values will be NaN.
Return value:
Return value type is int
.
Examples:
#
rnd_longrnd_long()
is used to return a random signed integer between0x8000000000000000L
and0x7fffffffffffffffL
.rnd_long(min, max, nanRate)
is used to generate long values in a specific range (for example only positive, or between 1 and 10), or to get occasionalNaN
values along with int values.
Arguments:
min
: is along
representing the lowest possible generated value (inclusive).max
: is along
representing the highest possible generated value (inclusive).nanRate
is anint
defining the frequency of occurrence ofNaN
values:0
: NoNaN
will be returned.1
: Will only returnNaN
.N > 1
: On average, one in N generated values will beNaN
.
Return value:
Return value type is long
.
Examples:
#
rnd_long256rnd_long256()
- generates a randomlong256
value between 0 and 2^256.
Return value:
Return value type is long256
.
Examples:
#
rnd_floatrnd_float()
- generates a random positivefloat
between 0 and 1.rnd_float(nanRate)
- generates a random positivefloat
between 0 and 1 which will beNaN
at a frequency defined bynanRate
.
Arguments:
nanRate
is anint
defining the frequency of occurrence ofNaN
values:0
: NoNaN
will be returned.1
: Will only returnNaN
.N > 1
: On average, one in N generated values will beNaN
.
Return value:
Return value type is float
.
Examples:
#
rnd_doublernd_double()
- generates a random positivedouble
between 0 and 1.rnd_double(nanRate)
- generates a random positivedouble
between 0 and 1 which will beNaN
at a frequency defined bynanRate
.
Arguments:
nanRate
is anint
defining the frequency of occurrence ofNaN
values:0
: NoNaN
will be returned.1
: Will only returnNaN
.N > 1
: On average, one in N generated values will beNaN
.
Return value:
Return value type is double
.
Examples:
#
rnd_date()rnd_date()
generates a random date betweenstart
andend
dates (both inclusive). IT will also generateNaN
values at a frequency defined bynanRate
. Whenstart
orend
are invalid dates, or whenstart
is superior toend
, it will returninvalid range
error. WhennanRate
is inferior to 0, it will returninvalid NAN rate
error.
Arguments:
start
is adate
defining the minimum possible generated date (inclusive)end
is adate
defining the maximum possible generated date (inclusive)nanRate
defines the frequency of occurrence ofNaN
values:0
: NoNaN
will be returned.1
: Will only returnNaN
.N > 1
: On average, one in N generated values will be NaN.
Return value:
Return value type is date
.
Examples:
#
rnd_timestamp()rnd_timestamp(start, end, nanRate)
generates a random timestamp betweenstart
andend
timestamps (both inclusive). It will also generateNaN
values at a frequency defined bynanRate
. Whenstart
orend
are invalid timestamps, or whenstart
is superior toend
, it will returninvalid range
error. WhennanRate
is inferior to 0, it will returninvalid NAN rate
error.
Arguments:
start
is atimestamp
defining the minimum possible generated timestamp (inclusive)end
is atimestamp
defining the maximum possible generated timestamp (inclusive)nanRate
defines the frequency of occurrence ofNaN
values:0
: NoNaN
will be returned.1
: Will only returnNaN
.N > 1
: On average, one in N generated values will be NaN.
Return value:
Return value type is timestamp
.
Examples:
#
SequencesTo generate increasing timestamps, please refer the page about row generators.
#
rnd_charrnd_char()
is used to generate a randomchar
which will be an uppercase character from the 26-letter A to Z alphabet. Letters from A to Z will be generated with equal probability.
Return value:
Return value type is char
.
Examples:
#
rnd_symbolrnd_symbol(symbolList)
is used to choose a randomsymbol
from a list defined by the user. It is useful when looking to generate specific symbols from a finite list (e.gBUY, SELL
orAUTUMN, WINTER, SPRING, SUMMER
. Symbols are randomly chosen from the list with equal probability. When only one symbol is provided in the list, this symbol will be chosen with 100% probability, in which case it is more efficient to usecast('your_symbol' as symbol
rnd_symbol(list_size, minLength, maxLength, nullRate)
generated a finite list of distinct random symbols and chooses one symbol from the list at random. The finite list is of sizelist_size
. The generated symbols length is betweenminLength
andmaxLength
(both inclusive). The function will also generatenull
values at a rate defined bynullRate
.
Arguments:
symbolList
is a variable-length list of possiblesymbol
values expressed as a comma-separated list of strings. For example,'a', 'bcd', 'efg123', '行'
list_size
is the number of distinctsymbol
values to generatedminLength
is anint
defining the minimum length for of a generated symbol (inclusive)maxLength
is anint
defining the maximum length for of a generated symbol (inclusive)nullRate
is anint
defining the frequency of occurrence ofnull
values:0
: Nonull
will be returned.1
: Will only returnnull
.N > 1
: On average, one in N generated values will benull
.
Return value:
Return value type is symbol
.
Examples:
#
rnd_strrnd_str(stringList)
is used to choose a randomstring
from a list defined by the user. It is useful when looking to generate specific strings from a finite list (e.gBUY, SELL
orAUTUMN, WINTER, SPRING, SUMMER
. Strings are randomly chosen from the list with equal probability. When only one string is provided in the list, this string will be chosen with 100% probability.rnd_str(list_size, minLength, maxLength, nullRate)
generated a finite list of distinct random string and chooses one string from the list at random. The finite list is of sizelist_size
. The generated strings length is betweenminLength
andmaxLength
(both inclusive). The function will also generatenull
values at a rate defined bynullRate
.
Arguments:
strList
is a variable-length list of possiblestring
values expressed as a comma-separated list of strings. For example,'a', 'bcd', 'efg123', '行'
list_size
is the number of distinctstring
values to generatedminLength
is anint
defining the minimum length for of a generated string (inclusive)maxLength
is anint
defining the maximum length for of a generated string (inclusive)nullRate
is anint
defining the frequency of occurrence ofnull
values:0
: Nonull
will be returned.1
: Will only returnnull
.N > 1
: On average, one in N generated values will benull
.
Return value:
Return value type is string
.
Examples:
#
rnd_binrnd_bin()
generates random binary data of a size up to32
bytes.rnd_bin(minBytes, maxBytes, nullRate)
generates random binary data of a size betweenminBytes
andmaxBytes
and returnsnull
at a rate defined bynullRate
.
Arguments:
minBytes
is along
defining the minimum size in bytes for of a generated binary (inclusive)maxBytes
is along
defining the maximum size in bytes for of a generated binary (inclusive)nullRate
is anint
defining the frequency of occurrence ofnull
values:0
: Nonull
will be returned.1
: Will only returnnull
.N > 1
: On average, one in N generated values will benull
.
Return value:
Return value type is binary
.
Examples: