Mersenne Twister - 5.2 English - 68552

AOCL API Guide (68552)

Document ID
68552
Release Date
2025-12-29
Version
5.2 English

The Mersenne Twister is a twisted generalized feedback shift register generator. The algorithm is as follows: * Set some arbitrary initial values \(x_1, x_2, · · · , x_r\), each consisting of w bits. * Letting

\[\begin{split}A = \begin{pmatrix} 0 & I_{w−1} \\ a_w & a_{w−1}...a_1 \\ \end{pmatrix}\end{split}\]

where \(\mathbf I_{w−1}\) is the \((w-1)\times(w-1)\) identity matrix and each of the \(a_i, i = 1\ to\ w\) take a value of either 0 or 1 (i.e. they can be represented as bits).

Define

\[\mathcal X_{i+r} = \mathbf(\mathcal X_{i+s} \oplus \mathbf( \mathcal X_{i}^{(w:(l+1))} | \mathcal X_{i+1}^{l-1})A )\]

where \(\mathcal X_{i}^{(w:(l+1))} | \mathcal X_{i+1}^{(l:1)}\) indicates the concatenation of the most significant (upper) \(w − l\) bits of \(\mathcal X_i\) and the least significant (lower) \(\mathcal l\) bits of \(\mathcal X_{i+1}\).

  • Perform the following operations sequentially:

\[\begin{split}\begin{align} \mathcal Z& = \mathcal X_{i+r} ⊕ (\mathcal X_{i+r} \gg t1) \\ \mathcal Z& = \mathcal Z ⊕ ((\mathcal Z \ll t2)\ AND\ m_1) \\ \mathcal Z& = \mathcal Z ⊕ ((\mathcal Z \ll t3)\ AND\ m_2) \\ \mathcal Z& = \mathcal Z ⊕ (\mathcal Z \gg t_4) \\ \mathcal u_{i+r}& = \frac{\mathcal Z}{(2^w − 1)}, \\ \end{align}\end{split}\]

where \(t_1, t_2, t_3\) and \(t_4\) are integers and \(m_1\) and \(m_2\) are bit-masks and >>t and <<t represent a t bit shift right and left respectively, is bit-wise exclusively or (xor) operation and AND is a bit-wise and operation.

The \(u_{i+r} : i = 1, 2,…\) then form a pseudo-random sequence, with \(u_i \in (0, 1)\), for all i. This implementation of the Mersenne Twister uses the following values for the algorithmic constants:

\[\begin{split}\begin{align} w& = 32 \\ a& = 0x9908b0df \\ l& = 31 \\ r& = 624 \\ s& = 397 \\ t_1& = 11 \\ t_2& = 7 \\ t_3& = 15 \\ t_4& = 18 \\ m_1& = 0x9d2c5680 \\ m_2& = 0xefc60000 \end{align}\end{split}\]

where the notation \(0 \times DD \dotsc`\) indicates the bit pattern of the integer whose hexadecimal representation is \(DD \dotsc\).

This algorithm has a period length of approximately \(2^{19,937} − 1\) and has been shown to be uniformly distributed in 623 dimensions. [MT]