---
title: "Random Numbers"
canonical: "https://docs.magma-maths.org/v2.30-1/BasicRings/Integers/random.html"
source: "https://docs.magma-maths.org/v2.30-1/BasicRings/Integers/random.md"
part: "Basic Rings"
chapter: "Ring of Integers"
breadcrumbs:
  - {"title": "Magma Handbook", "source": "https://docs.magma-maths.org/v2.30-1/index.md"}
  - {"title": "Basic Rings", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/index-basic-rings.md"}
  - {"title": "Ring of Integers", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/Integers/index-integers.md"}
prev: {"title": "Element Operations", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/Integers/operation-element.md"}
next: {"title": "GCD and LCM", "source": "https://docs.magma-maths.org/v2.30-1/BasicRings/Integers/gcd-lcm.md"}
intrinsics:
  - {"name": "Random", "signature": "Random(a, b): RngIntElt, RngIntElt -> RngIntElt"}
  - {"name": "Random", "signature": "Random(b): RngIntElt -> RngIntElt"}
  - {"name": "RandomBits", "signature": "RandomBits(n): RngIntElt -> RngIntElt"}
  - {"name": "RandomPrime", "signature": "RandomPrime(n: parameter): RngIntElt -> RngIntElt"}
  - {"name": "RandomPrime", "signature": "RandomPrime(n, a, b, x: parameter): RngIntElt, RngIntElt, RngIntElt -> BoolElt, RngIntElt"}
  - {"name": "RandomConsecutiveBits", "signature": "RandomConsecutiveBits(n, a, b): RngIntElt, RngIntElt -> RngIntElt"}
---

# Random Numbers

Pseudo-random integers in Magma are generated using the *Monster* random number generator of G. Marsaglia [[Mar00](https://docs.magma-maths.org/v2.30-1/references.md#cite-marsaglia)] (see also [[MZ91](https://docs.magma-maths.org/v2.30-1/references.md#cite-marsagliazaman)] and [[Mar92](https://docs.magma-maths.org/v2.30-1/references.md#cite-marsaglia92)]). The period of the Monster generator is $2^{29430} - 2^{27382}$ (approximately $10^{8859}$), and the generator passes all of the stringent tests in Marsaglia’s *Diehard* test suite [[Mar95](https://docs.magma-maths.org/v2.30-1/references.md#cite-marsagliadiehard)]. Throughout the following text, the word ‘random’ is used to mean ‘pseudo-random’.

<a id="function-random-rngintelt-rngintelt"></a>
## `Random(a, b): RngIntElt, RngIntElt -> RngIntElt`

A random integer lying in the interval $[a, b]$, where $a\leq b$.

<a id="function-random-rngintelt"></a>
## `Random(b): RngIntElt -> RngIntElt`

A random integer lying in the interval $[0, b]$, where $b$ is a non-negative integer. Because of the good properties of the underlying Monster generator, calling `Random(1)` is a good safe way of producing a sequence of random bits.

<a id="function-randombits-rngintelt"></a>
## `RandomBits(n): RngIntElt -> RngIntElt`

A random integer $m$ such that $0 \leq m < 2^n$, where $n$ is a small non-negative integer. Thus, $m$ has $n$ random bits with a probability of 1/2 for each bit. The function always returns $0$ when $n=0$.

<a id="function-randomprime-rngintelt"></a>
## `RandomPrime(n: parameter): RngIntElt -> RngIntElt`

```magma
Proof: BoolElt                    Default: true
```

A random prime integer $m$ such that $0 < m < 2^n$, where $n$ is a small non-negative integer. The function always returns $0$ for $n=0$ or $n=1$. A rigorous method will be used to check primality, unless $m > 3317044064679887385961981$ and the optional parameter `Proof` is set to `Proof := false`, in which case the result indicates that $m$ is a probable prime (of order 20).

<a id="function-randomprime-rngintelt-rngintelt-rngintelt"></a>
## `RandomPrime(n, a, b, x: parameter): RngIntElt, RngIntElt, RngIntElt -> BoolElt, RngIntElt`

```magma
Proof: BoolElt                    Default: true
```

Tries up to $x$ iterations to find a random prime integer $m$ congruent to $a$ modulo $b$ such that $0 < m < 2^n$. If successful, the function returns true and the integer $m$, otherwise false. The integer $n$ must be larger than 0, $a$ must lie between 0 and $b-1$ and $b$ must be larger than $0$. A rigorous method will be used to establish primality, unless $m > 3317044064679887385961981$ and the optional parameter `Proof` is set to `Proof := false`, in which case the result indicates that $m$ is a probable prime (of order 20).

<a id="function-randomconsecutivebits-rngintelt-rngintelt"></a>
## `RandomConsecutiveBits(n, a, b): RngIntElt, RngIntElt -> RngIntElt`

A integer $m$ such that $0 \leq m < 2^n$, and the binary expansion of $n$ consists of consecutive strings of zeros or ones each of random length in the range $[a \ldots b]$.
