Functions for generating random numbers

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/functions/random-functions.md)

All functions in this section accept zero or one arguments. The only use of the argument (if provided) is to prevent [common subexpression
elimination](/docs/sql-reference/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](/docs/guides/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

The documentation below is generated from the `system.functions` system table.

## fuzzBits[​](#fuzzBits "Direct link to fuzzBits")

Introduced in: v20.5.0

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

**Syntax**

```
fuzzBits(s, p)
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT fuzzBits(materialize('abacaba'), 0.1)
FROM numbers(3)
```

```
┌─fuzzBits(materialize('abacaba'), 0.1)─┐
│ abaaaja                               │
│ a*cjab+                               │
│ aeca2A                                │
└───────────────────────────────────────┘
```

## rand[​](#rand "Direct link to rand")

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

```
rand([x])
```

**Aliases**: `rand32`

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT rand();
```

```
1569354847
```

## rand64[​](#rand64 "Direct link to rand64")

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

```
rand64([x])
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT rand64();
```

```
15030268859237645412
```

## randBernoulli[​](#randBernoulli "Direct link to randBernoulli")

Introduced in: v22.10.0

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

**Syntax**

```
randBernoulli(probability[, x])
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT randBernoulli(.75) FROM numbers(5)
```

```
┌─randBernoulli(0.75)─┐
│                   1 │
│                   1 │
│                   0 │
│                   1 │
│                   1 │
└─────────────────────┘
```

## randBinomial[​](#randBinomial "Direct link to randBinomial")

Introduced in: v22.10.0

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

**Syntax**

```
randBinomial(experiments, probability[, x])
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT randBinomial(100, .75) FROM numbers(5)
```

```
┌─randBinomial(100, 0.75)─┐
│                      74 │
│                      78 │
│                      76 │
│                      77 │
│                      80 │
└─────────────────────────┘
```

## randCanonical[​](#randCanonical "Direct link to randCanonical")

Introduced in: v22.11.0

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

**Syntax**

```
randCanonical([x])
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT randCanonical();
```

```
0.345217890123456
```

## randChiSquared[​](#randChiSquared "Direct link to randChiSquared")

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

```
randChiSquared(degree_of_freedom[, x])
```

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT randChiSquared(10) FROM numbers(5)
```

```
┌─randChiSquared(10)─┐
│ 10.015463656521543 │
│  9.621799919882768 │
│   2.71785015634699 │
│ 11.128188665931908 │
│  4.902063104425469 │
└────────────────────┘
```