- Data simulation
- Testing
- Statistical analysis
ClickHouse function reference
rand
Generates a randomUInt32
number with uniform distribution.
Syntax:
- rand32
UInt32
number.
Example:
This function uses a linear congruential generator with an initial state obtained from the system. While it appears random, it’s not cryptographically secure. For scenarios requiring true randomness, consider using alternative methods.
rand64
Generates a randomUInt64
number with uniform distribution.
Syntax:
UInt64
number.
Example:
Like
rand()
, this function uses a linear congruential generator and is not suitable for cryptographic purposes.randCanonical
Returns a randomFloat64
number between 0 (inclusive) and 1 (exclusive) with uniform distribution.
Syntax:
Float64
value in the range [0, 1).
Example:
Float64
number each time the query is executed.
This function is useful for generating random floating-point numbers for various purposes, such as simulations, random sampling, or creating test data. Keep in mind that the generated numbers are not cryptographically secure and should not be used for cryptographic purposes.
randConstant
Generates a constant randomUInt32
value that remains the same for all rows in the query result.
Syntax:
x
(optional): Any value. If provided, it influences the generated random value, but the result will still be constant within the same query execution.
UInt32
constant random value.
Example:
randConstant()
generates the same random taco ID for all rows, which is then used to create unique taco names.
- The actual random value will be different for each query execution.
- Using different arguments may not significantly change the generated value compared to using
randConstant()
without arguments. - This function is useful when you need a consistent random value across all rows in a single query, such as for generating test data or creating a random identifier for a batch operation.
randUniform
Generates a random Float64 number uniformly distributed between the specified minimum and maximum values. Syntax:min
(Float64
): The lower bound of the range (inclusive).max
(Float64
): The upper bound of the range (inclusive).
Float64
number uniformly distributed between min
and max
.
Example:
The actual output will consist of different random numbers within the specified range.
randNormal
Generates a random Float64 number drawn from a normal distribution (also known as Gaussian distribution). Syntax:mean
(Float64
): The mean (average) value of the distribution.variance
(Float64
): The variance of the distribution.
- A random number from the normal distribution. [
Float64
]
The normal distribution is symmetric around its mean, and the variance determines how spread out the values are. Approximately 68% of the values will fall within one standard deviation (square root of variance) of the mean.
randLogNormal
Returns a random Float64 number drawn from a log-normal distribution. Syntax:mean
(Float64
): Mean value of the distribution.variance
(Float64
): Variance of the distribution.
- A random number from the log-normal distribution.
Float64
.
The actual output will consist of different random numbers each time the query is executed.
randBinomial
Generates a random UInt64 number drawn from a binomial distribution. Syntax:experiments
(UInt64
): Number of experiments.probability
(Float64
): Probability of success in each experiment, a value between 0 and 1.
- A random number following the binomial distribution. (
UInt64
)
The actual output will consist of random numbers and may differ from the example shown.
randNegativeBinomial
Returns a random UInt64 number drawn from a negative binomial distribution. Syntax:experiments
(UInt64
): Number of experiments.probability
(Float64
): Probability of failure in each experiment, a value between 0 and 1.
- A random number following the negative binomial distribution. (
UInt64
)
The negative binomial distribution models the number of failures before a specified number of successes occurs in a sequence of independent trials. It’s useful for modeling scenarios like the number of attempts needed to achieve a certain number of successful outcomes.
randPoisson
Generates a random UInt64 number drawn from a Poisson distribution. Syntax:n
(UInt64
): Mean number of occurrences.
- A random number following the Poisson distribution. [
UInt64
]
The actual output will vary due to the random nature of the function.
randBernoulli
Generates a randomUInt64
value drawn from a Bernoulli distribution.
Syntax:
probability
(Float64
): A value representing the probability of success, between 0 and 1.
- A random
UInt64
number: 1 (success) or 0 (failure).
The actual results will vary due to the random nature of the function.
randExponential
Generates a random Float64 number drawn from an exponential distribution. Syntax:lambda
(Float64
): Parameter of the exponential distribution. Must be greater than 0.
Float64
number from the exponential distribution.
Example:
The exponential distribution is often used to model the time between independent events that happen at a constant average rate, such as customer arrivals or equipment failures.
randChiSquared
Returns a randomFloat64
drawn from a Chi-square distribution - a distribution of a sum of the squares of k independent standard normal random variables.
Syntax:
degree_of_freedom
(Float64
): degree of freedom.
Float64
.
Example:
The actual output will consist of different random numbers each time the query is executed, not the specific numbers shown in the example.
randStudentT
Generates a random Float64 number drawn from a Student’s t-distribution. Syntax:degree_of_freedom
(Float64
): A Float64 value representing the degrees of freedom for the t-distribution.
- A random Float64 number from the Student’s t-distribution.
The Student’s t-distribution is often used in statistical analysis, particularly when dealing with small sample sizes or when the population standard deviation is unknown.
d1
(Float64
): First degree of freedom for the F-distribution.d2
(Float64
): Second degree of freedom for the F-distribution.
Float64
.
Example:
d1
(10) might represent the number of taste testers and d2
(3) the number of taco varieties being compared.
The F-distribution is often used in statistical analysis, particularly in analysis of variance (ANOVA) and regression analysis. In the context of our taco example, it could be used to model the variability in taco quality ratings across different taco shops or recipes.
randomString
Generates a string of the specified length filled with random bytes (including zero bytes). Syntax:length
(UInt64
): String length in bytes. Positive integer.
String
].
Not all characters in the generated string may be printable.
length
function confirms that each generated string is indeed 10 bytes long.
The generated strings contain random bytes, which may include non-printable characters. In practice, you might want to use
randomPrintableASCII
for generating readable random strings.randomFixedString
Generates a binary string of the specified length filled with random bytes (including zero bytes). Syntax:length
(UInt64
): String length in bytes.
- A string filled with random bytes. [
FixedString
]
The generated string may contain any byte values, including null bytes and non-ASCII characters. Not all characters in the result may be printable.
randomPrintableASCII
Generates a string with a random set of printable ASCII characters. Syntax:length
(UInt64
): Desired string length in bytes. Positive integer.
String
]
Example:
If you pass a negative length, the behavior of the function is undefined.
randomStringUTF8
Generates a random string of a specified length containing valid UTF-8 code points. Syntax:length
(UInt64
): Length of the string in code points.
- A random UTF-8 string. [
String
]
The generated code points may be outside the range of assigned Unicode characters, potentially resulting in strings with unusual or non-printable characters.
fuzzBits
Flips random bits in a string with a specified probability. Syntax:s
(String
orFixedString
): The input string to be fuzzed.prob
(Float32
orFloat64
): The probability of flipping each bit, a constant value between 0.0 and 1.0.
s
.
Example:
fuzzBits
to the string ‘Crunchy Taco’ with a 10% probability of flipping each bit. The results show how random bits in the string may be flipped, potentially altering characters.
The function modifies bits randomly, so the output may contain non-printable characters or invalid UTF-8 sequences. The example above shows printable results for clarity, but actual outputs may vary.