> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for functions used to generate random numbers

# 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](/reference/functions/regular-functions/overview#common-subexpression-elimination) such that two different executions within a row of the same random
function return different random values.

Related content

* Guide: [Generating random data in ClickHouse](/guides/clickhouse/data-modelling/generating-test-data)
* Blog: [Generating random data in ClickHouse](https://clickhouse.com/blog/generating-random-test-distribution-data-for-clickhouse)

<Note>
  The random numbers are generated by non-cryptographic algorithms.
</Note>

<Note>
  The documentation below is generated from the `system.functions` system table.
</Note>

{/*AUTOGENERATED_START*/}

<h2 id="fuzzBits">
  fuzzBits
</h2>

Introduced in: v20.5.0

Flips the bits of the input string `s`, with probability `p` for each bit.

**Syntax**

```sql theme={null}
fuzzBits(s, p)
```

**Arguments**

* `s` — String or FixedString to perform bit fuzzing on [`String`](/reference/data-types/string) or [`FixedString`](/reference/data-types/fixedstring)
* `p` — Probability of flipping each bit as a number between `0.0` and `1.0` [`Float*`](/reference/data-types/float)

**Returned value**

Returns a Fuzzed string with same type as `s`. [`String`](/reference/data-types/string) or [`FixedString`](/reference/data-types/fixedstring)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT fuzzBits(materialize('abacaba'), 0.1)
FROM numbers(3)
```

```response title=Response theme={null}
┌─fuzzBits(materialize('abacaba'), 0.1)─┐
│ abaaaja                               │
│ a*cjab+                               │
│ aeca2A                                │
└───────────────────────────────────────┘
```

<h2 id="rand">
  rand
</h2>

Introduced in: v1.1.0

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**

```sql theme={null}
rand([x])
```

**Aliases**: `rand32`

**Arguments**

* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random number of type `UInt32`. [`UInt32`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT rand();
```

```response title=Response theme={null}
1569354847
```

<h2 id="rand64">
  rand64
</h2>

Introduced in: v1.1.0

Returns a random distributed `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.

**Syntax**

```sql theme={null}
rand64([x])
```

**Arguments**

* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random UInt64 number with uniform distribution. [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT rand64();
```

```response title=Response theme={null}
15030268859237645412
```

<h2 id="randBernoulli">
  randBernoulli
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from a [Bernoulli distribution](https://en.wikipedia.org/wiki/Bernoulli_distribution).

**Syntax**

```sql theme={null}
randBernoulli(probability[, x])
```

**Arguments**

* `probability` — The probability of success as a value between `0` and `1`. [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified Bernoulli distribution. [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randBernoulli(.75) FROM numbers(5)
```

```response title=Response theme={null}
┌─randBernoulli(0.75)─┐
│                   1 │
│                   1 │
│                   0 │
│                   1 │
│                   1 │
└─────────────────────┘
```

<h2 id="randBinomial">
  randBinomial
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from a [binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution).

**Syntax**

```sql theme={null}
randBinomial(experiments, probability[, x])
```

**Arguments**

* `experiments` — The number of experiments [`UInt64`](/reference/data-types/int-uint)
* `probability` — The probability of success in each experiment as a value between `0` and `1` [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified binomial distribution. [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randBinomial(100, .75) FROM numbers(5)
```

```response title=Response theme={null}
┌─randBinomial(100, 0.75)─┐
│                      74 │
│                      78 │
│                      76 │
│                      77 │
│                      80 │
└─────────────────────────┘
```

<h2 id="randCanonical">
  randCanonical
</h2>

Introduced in: v22.11.0

Returns a random distributed `Float64` number with uniform distribution between `0` (inclusive) and `1` (exclusive).

**Syntax**

```sql theme={null}
randCanonical([x])
```

**Arguments**

* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number. [`Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randCanonical();
```

```response title=Response theme={null}
0.345217890123456
```

<h2 id="randChiSquared">
  randChiSquared
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from a [chi-square distribution](https://en.wikipedia.org/wiki/Chi-squared_distribution).

**Syntax**

```sql theme={null}
randChiSquared(degree_of_freedom[, x])
```

**Arguments**

* `degree_of_freedom` — Degrees of freedom. [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified chi-square distribution. [`Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randChiSquared(10) FROM numbers(5)
```

```response title=Response theme={null}
┌─randChiSquared(10)─┐
│ 10.015463656521543 │
│  9.621799919882768 │
│   2.71785015634699 │
│ 11.128188665931908 │
│  4.902063104425469 │
└────────────────────┘
```

<h2 id="randConstant">
  randConstant
</h2>

Introduced in: v1.1.0

Generates a single random value that remains constant across all rows in the current query execution.

This function:

* Returns the same random value for every row within a single query
* Produces different values across separate query executions

It is useful for applying consistent random seeds or identifiers across all rows in a dataset

**Syntax**

```sql theme={null}
randConstant([x])
```

**Arguments**

* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a column of type `UInt32` containing the same random value in each row. [`UInt32`](/reference/data-types/int-uint)

**Examples**

**Basic usage**

```sql title=Query theme={null}
SELECT randConstant() AS random_value;
```

```response title=Response theme={null}
| random_value |
|--------------|
| 1234567890   |
```

**Usage with parameter**

```sql title=Query theme={null}
SELECT randConstant(10) AS random_value;
```

```response title=Response theme={null}
| random_value |
|--------------|
| 9876543210   |
```

<h2 id="randExponential">
  randExponential
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from an [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution).

**Syntax**

```sql theme={null}
randExponential(lambda[, x])
```

**Arguments**

* `lambda` — Rate parameter or lambda value of the distribution [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified exponential distribution. [`Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randExponential(1/10) FROM numbers(5)
```

```response title=Response theme={null}
┌─randExponential(divide(1, 10))─┐
│              44.71628934340778 │
│              4.211013337903262 │
│             10.809402553207766 │
│              15.63959406553284 │
│             1.8148392319860158 │
└────────────────────────────────┘
```

<h2 id="randFisherF">
  randFisherF
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from an [F-distribution](https://en.wikipedia.org/wiki/F-distribution).

**Syntax**

```sql theme={null}
randFisherF(d1, d2[, x])
```

**Arguments**

* `d1` — d1 degree of freedom in `X = (S1 / d1) / (S2 / d2)`. [`Float64`](/reference/data-types/float)
* `d2` — d2 degree of freedom in `X = (S1 / d1) / (S2 / d2)`. [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified F-distribution [`Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randFisherF(10, 3) FROM numbers(5)
```

```response title=Response theme={null}
┌─randFisherF(10, 20)─┐
│  0.7204609609506184 │
│  0.9926258472572916 │
│  1.4010752726735863 │
│ 0.34928401507025556 │
│  1.8216216009473598 │
└─────────────────────┘
```

<h2 id="randLogNormal">
  randLogNormal
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from a [log-normal distribution](https://en.wikipedia.org/wiki/Log-normal_distribution).

**Syntax**

```sql theme={null}
randLogNormal(mean, stddev[, x])
```

**Arguments**

* `mean` — The mean value of distribution. [`Float64`](/reference/data-types/float)
* `stddev` — The standard deviation of the distribution. [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified log-normal distribution. [`Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randLogNormal(100, 5) FROM numbers(5)
```

```response title=Response theme={null}
┌─randLogNormal(100, 5)─┐
│  1.295699673937363e48 │
│  9.719869109186684e39 │
│  6.110868203189557e42 │
│  9.912675872925529e39 │
│ 2.3564708490552458e42 │
└───────────────────────┘
```

<h2 id="randNegativeBinomial">
  randNegativeBinomial
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from a [negative binomial distribution](https://en.wikipedia.org/wiki/Negative_binomial_distribution).

**Syntax**

```sql theme={null}
randNegativeBinomial(experiments, probability[, x])
```

**Arguments**

* `experiments` — The number of experiments. [`UInt64`](/reference/data-types/int-uint)
* `probability` — `The probability of failure in each experiment as a value between `0`and`1`. [`Float64\`]\(/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified negative binomial distribution [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randNegativeBinomial(100, .75) FROM numbers(5)
```

```response title=Response theme={null}
┌─randNegativeBinomial(100, 0.75)─┐
│                              33 │
│                              32 │
│                              39 │
│                              40 │
│                              50 │
└─────────────────────────────────┘
```

<h2 id="randNormal">
  randNormal
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from a [normal distribution](https://en.wikipedia.org/wiki/Normal_distribution).

**Syntax**

```sql theme={null}
randNormal(mean, stddev[, x])
```

**Arguments**

* `mean` — The mean value of distribution [`Float64`](/reference/data-types/float)
* `stddev` — The standard deviation of the distribution [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified normal distribution. [`Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randNormal(10, 2) FROM numbers(5)
```

```response title=Response theme={null}
┌──randNormal(10, 2)─┐
│ 13.389228911709653 │
│  8.622949707401295 │
│ 10.801887062682981 │
│ 4.5220192605895315 │
│ 10.901239123982567 │
└────────────────────┘
```

<h2 id="randPoisson">
  randPoisson
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from a [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution) distribution.

**Syntax**

```sql theme={null}
randPoisson(n[, x])
```

**Arguments**

* `n` — The mean number of occurrences. [`UInt64`](/reference/data-types/int-uint)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified Poisson distribution. [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randPoisson(10) FROM numbers(5)
```

```response title=Response theme={null}
┌─randPoisson(10)─┐
│               8 │
│               8 │
│               7 │
│              10 │
│               6 │
└─────────────────┘
```

<h2 id="randStudentT">
  randStudentT
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn from a [Student's t-distribution](https://en.wikipedia.org/wiki/Student%27s_t-distribution).

**Syntax**

```sql theme={null}
randStudentT(degree_of_freedom[, x])
```

**Arguments**

* `degree_of_freedom` — Degrees of freedom. [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random Float64 number drawn from the specified Student's t-distribution. [`Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randStudentT(10) FROM numbers(5)
```

```response title=Response theme={null}
┌─────randStudentT(10)─┐
│   1.2217309938538725 │
│   1.7941971681200541 │
│ -0.28192176076784664 │
│   0.2508897721303792 │
│  -2.7858432909761186 │
└──────────────────────┘
```

<h2 id="randUniform">
  randUniform
</h2>

Introduced in: v22.10.0

Returns a random Float64 number drawn uniformly from the interval $[\min, \max]$.

**Syntax**

```sql theme={null}
randUniform(min, max[, x])
```

**Arguments**

* `min` — Left boundary of the range (inclusive). [`Float64`](/reference/data-types/float)
* `max` — Right boundary of the range (inclusive). [`Float64`](/reference/data-types/float)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a random number drawn uniformly from the interval formed by `min` and `max`. [`Float64`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randUniform(5.5, 10) FROM numbers(5)
```

```response title=Response theme={null}
┌─randUniform(5.5, 10)─┐
│    8.094978491443102 │
│   7.3181248914450885 │
│    7.177741903868262 │
│    6.483347380953762 │
│    6.122286382885112 │
└──────────────────────┘
```

<h2 id="randomFixedString">
  randomFixedString
</h2>

Introduced in: v20.5.0

Generates a random fixed-size string with the specified number of character.
The returned characters are not necessarily ASCII characters, i.e. they may not be printable.

**Syntax**

```sql theme={null}
randomFixedString(length)
```

**Arguments**

* `length` — Length of the string in bytes. [`UInt*`](/reference/data-types/int-uint)

**Returned value**

Returns a string filled with random bytes. [`FixedString`](/reference/data-types/fixedstring)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randomFixedString(13) AS rnd, toTypeName(rnd)
```

```response title=Response theme={null}
┌─rnd──────┬─toTypeName(randomFixedString(13))─┐
│ j▒h㋖HɨZ'▒ │ FixedString(13)                 │
└──────────┴───────────────────────────────────┘
```

<h2 id="randomPrintableASCII">
  randomPrintableASCII
</h2>

Introduced in: v20.1.0

Generates a random [ASCII](https://en.wikipedia.org/wiki/ASCII#Printable_characters) string with the specified number of characters.

If you pass `length < 0`, the behavior of the function is undefined.

**Syntax**

```sql theme={null}
randomPrintableASCII(length[, x])
```

**Arguments**

* `length` — String length in bytes. [`(U)Int*`](/reference/data-types/int-uint)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a string with a random set of ASCII printable characters. [`String`](/reference/data-types/string)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT number, randomPrintableASCII(30) AS str, length(str) FROM system.numbers LIMIT 3
```

```response title=Response theme={null}
┌─number─┬─str────────────────────────────┬─length(randomPrintableASCII(30))─┐
│      0 │ SuiCOSTvC0csfABSw=UcSzp2.`rv8x │                               30 │
│      1 │ 1Ag NlJ &RCN:*>HVPG;PE-nO"SUFD │                               30 │
│      2 │ /"+<"with:=LjJ Vm!c&hI*m#XTfzz │                               30 │
└────────┴────────────────────────────────┴──────────────────────────────────┘
```

<h2 id="randomString">
  randomString
</h2>

Introduced in: v20.5.0

Generates a random string with the specified number of characters.
The returned characters are not necessarily ASCII characters, i.e. they may not be printable.

**Syntax**

```sql theme={null}
randomString(length[, x])
```

**Arguments**

* `length` — Length of the string in bytes. [`(U)Int*`](/reference/data-types/int-uint)
* `x` — Optional and ignored. The only purpose of the argument is to prevent [common subexpression elimination](/reference/functions/regular-functions/overview#common-subexpression-elimination) when the same function call is used multiple times in a query. [`Any`](/reference/data-types/index)

**Returned value**

Returns a string filled with random bytes. [`String`](/reference/data-types/string)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randomString(5) AS str FROM numbers(2)
```

```response title=Response theme={null}
���
�v6B�
```

<h2 id="randomStringUTF8">
  randomStringUTF8
</h2>

Introduced in: v20.5.0

Generates a random [UTF-8](https://en.wikipedia.org/wiki/UTF-8) string with the specified number of codepoints.
No codepoints from unassigned [planes](https://en.wikipedia.org/wiki/Plane_\(Unicode\)) (planes 4 to 13) are returned.
It is still possible that the client interacting with ClickHouse server is not able to display the produced UTF-8 string correctly.

**Syntax**

```sql theme={null}
randomStringUTF8(length)
```

**Arguments**

* `length` — Length of the string in code points. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns a string filled with random UTF-8 codepoints. [`String`](/reference/data-types/string)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT randomStringUTF8(13)
```

```response title=Response theme={null}
┌─randomStringUTF8(13)─┐
│ 𘤗𙉝д兠庇󡅴󱱎󦐪􂕌𔊹𓰛       │
└──────────────────────┘
```
