Subtract with carry

From HandWiki
Short description: Pseudorandom number generator algorithm

Subtract-with-carry (SWC) is a pseudorandom number generator created by George Marsaglia and Arif Zaman in 1991.[1] It falls into a class of generators known as lagged Fibonacci generators, where each new number in the sequence is a function of two previous numbers at fixed distances ("lags").

SWC is one of three random number generator engines included in the standard C++11 library.[2] It belongs to a family of generators that also includes add-with-carry and subtract-with-borrow engines.[1]

Algorithm

The subtract-with-carry algorithm's state is defined by a list of R numbers and a "carry" value, where R is the "long lag". The initial values for this state, known as the "seed," can be chosen arbitrarily.

To generate the next number in the sequence, the algorithm uses two values from its state list: the value at the "short lag" position (S steps ago) and the value at the "long lag" position (R steps ago). The new number is calculated by subtracting the long-lag value and the current carry bit from the short-lag value.[3]

If this subtraction results in a negative number (a "borrow"), the result is adjusted by adding a large constant M (the modulus), and the carry for the next step is set to 1. Otherwise, the carry is set to 0. The newly generated number replaces the oldest number in the list, and the process repeats.

Example

A simple example can illustrate the process. Let the parameters be:

  • Modulus M = 10
  • Long lag R = 3
  • Short lag S = 1
  • Initial state (seed): a list of 3 numbers x=(x0,x1,x2)=(6,8,3) and an initial carry c2=0.

To generate the next number, x3:

  1. Identify the short-lag value x3S=x2=3 and the long-lag value x3R=x0=6.
  2. Perform the subtraction: x2x0c2360=3.
  3. Since the result is negative, a borrow occurs. The new carry c3 becomes 1.
  4. The new number x3 is the result modulo M: 3mod10=7.
  5. The state is updated. The list becomes (x1,x2,x3)=(8,3,7), and the carry is now 1 for the next step.

This process can be repeated to generate a long sequence of pseudorandom numbers.

Formal definition

The sequence generated by the subtract-with-carry engine is described by the recurrence relation:

x(i)=(x(iS)x(iR)cy(i1)) mod M

where the new carry, cy(i), is defined as:

cy(i)={1,if x(iS)x(iR)cy(i1)<00,otherwise

The lags must satisfy the condition 0<S<R. The modulus M is typically a power of 2, such as M=2W, where W is the word size of the state sequence in bits.[1]

References

  1. 1.0 1.1 1.2 A New Class of Random Number Generators, George Marsaglia and Arif Zaman, The Annals of Applied Probability, Vol. 1, No. 3, 1991
  2. std::subtract_with_carry_engine, cppreference.com
  3. subtract_with_carry_engine Class, Microsoft Visual Studio 2015