Functions for Generating Random Numbers
All functions in this section accept zero or one arguments. The only use of the argument (if provided) is to prevent common subexpression elimination such that two different executions within a row of the same random function return different random values.
Related content
The random numbers are generated by non-cryptographic algorithms.
rand
Returns a random UInt32 number with uniform distribution.
Uses a linear congruential generator with an initial state obtained from the system, which means that while it appears random, it's not truly random and can be predictable if the initial state is known. For scenarios where true randomness is crucial, consider using alternative methods like system-level calls or integrating with external libraries.
Syntax
rand()
Alias: rand32
Arguments
None.
Returned value
Returns a number of type UInt32.
Example
SELECT rand();
1569354847 -- Note: The actual output will be a random number, not the specific number shown in the example
rand64
Returns a random UInt64 integer (UInt64) number
Syntax
rand64()
Arguments
None.
Arguments
Returns a number UInt64 number with uniform distribution.
Uses a linear congruential generator with an initial state obtained from the system, which means that while it appears random, it's not truly random and can be predictable if the initial state is known. For scenarios where true randomness is crucial, consider using alternative methods like system-level calls or integrating with external libraries.
Example
SELECT rand64();
15030268859237645412 -- Note: The actual output will be a random number, not the specific number shown in the example.
randCanonical
Returns a random Float64 number.
Syntax
randCanonical()
Arguments
None.
Arguments
Returns a Float64 value between 0 (inclusive) and 1 (exclusive).
Example
SELECT randCanonical();
0.3452178901234567 - Note: The actual output will be a random Float64 number between 0 and 1, not the specific number shown in the example.
randConstant
Generates a single constant column filled with a random value. Unlike rand
, this function ensures the same random value appears in every row of the generated column, making it useful for scenarios requiring a consistent random seed across rows in a single query.
Syntax
randConstant([x]);
Arguments
- [x] (Optional): An optional expression that influences the generated random value. Even if provided, the resulting value will still be constant within the same query execution. Different queries using the same expression will likely generate different constant values.
Arguments
Returns a column of type UInt32 containing the same random value in each row.
Implementation details
The actual output will be different for each query execution, even with the same optional expression. The optional parameter may not significantly change the generated value compared to using randConstant
alone.
Example
SELECT randConstant() AS random_value;
| random_value |
|--------------|
| 1234567890 |
SELECT randConstant(10) AS random_value;
| random_value |
|--------------|
| 9876543210 |
randUniform
Returns a random Float64 drawn uniformly from interval [min
, max
].
Syntax
randUniform(min, max)
Arguments
min
-Float64
- left boundary of the range,max
-Float64
- right boundary of the range.
Arguments
A random number of type Float64.
Example
SELECT randUniform(5.5, 10) FROM numbers(5)
┌─randUniform(5.5, 10)─┐
│ 8.094978491443102 │
│ 7.3181248914450885 │
│ 7.177741903868262 │
│ 6.483347380953762 │
│ 6.122286382885112 │
└──────────────────────┘
randNormal
Returns a random Float64 drawn from a normal distribution.
Syntax
randNormal(mean, variance)
Arguments
mean
-Float64
- mean value of distribution,variance
-Float64
- variance of the distribution.
Returned value
- Random number. Float64.
Example
SELECT randNormal(10, 2) FROM numbers(5)
Result:
┌──randNormal(10, 2)─┐
│ 13.389228911709653 │
│ 8.622949707401295 │
│ 10.801887062682981 │
│ 4.5220192605895315 │
│ 10.901239123982567 │
└────────────────────┘
randLogNormal
Returns a random Float64 drawn from a log-normal distribution.
Syntax
randLogNormal(mean, variance)
Arguments
mean
-Float64
- mean value of distribution,variance
-Float64
- variance of the distribution.
Returned value
- Random number. Float64.
Example
SELECT randLogNormal(100, 5) FROM numbers(5)
Result:
┌─randLogNormal(100, 5)─┐
│ 1.295699673937363e48 │
│ 9.719869109186684e39 │
│ 6.110868203189557e42 │
│ 9.912675872925529e39 │
│ 2.3564708490552458e42 │
└───────────────────────┘
randBinomial
Returns a random UInt64 drawn from a binomial distribution.
Syntax
randBinomial(experiments, probability)
Arguments
experiments
-UInt64
- number of experiments,probability
-Float64
- probability of success in each experiment, a value between 0 and 1.
Returned value
- Random number. UInt64.
Example
SELECT randBinomial(100, .75) FROM numbers(5)
Result:
┌─randBinomial(100, 0.75)─┐
│ 74 │
│ 78 │
│ 76 │
│ 77 │
│ 80 │
└─────────────────────────┘
randNegativeBinomial
Returns a random UInt64 drawn from a negative binomial distribution.
Syntax
randNegativeBinomial(experiments, probability)
Arguments
experiments
-UInt64
- number of experiments,probability
-Float64
- probability of failure in each experiment, a value between 0 and 1.
Returned value
- Random number. UInt64.
Example
SELECT randNegativeBinomial(100, .75) FROM numbers(5)
Result:
┌─randNegativeBinomial(100, 0.75)─┐
│ 33 │
│ 32 │
│ 39 │
│ 40 │
│ 50 │
└─────────────────────────────────┘
randPoisson
Returns a random UInt64 drawn from a Poisson distribution.
Syntax
randPoisson(n)
Arguments
n
-UInt64
- mean number of occurrences.
Returned value
- Random number. UInt64.
Example
SELECT randPoisson(10) FROM numbers(5)
Result:
┌─randPoisson(10)─┐
│ 8 │
│ 8 │