# std::linear_congruential_engine

> C++ Atlas: `cppatlas.com/w/cpp/numeric/random/linear_congruential_engine` | Source: https://en.cppreference.com/w/cpp/numeric/random/linear_congruential_engine | Domain: Numerics | Standards: C++11, C++20

linear_congruential_engine is a random number engine based on Linear congruential generator (LCG).

## Official Specification Summary (cppreference)

```cpp
// Defined in header <random>

template<
    class UIntType,
    UIntType a,
    UIntType c,
    UIntType m
> class linear_congruential_engine; // (since C++11)
```

`linear_congruential_engine` is a random number engine based on Linear congruential generator (LCG).

### Template parameters

- `UIntType` - The result type generated by the generator. The effect is undefined if this is not one of `unsigned short`, `unsigned int`, `unsigned long`, or `unsigned long long`.
- `a` - the multiplier term
- `c` - the increment term
- `m` - the modulus term

When `m` is not zero, if `a >= m` or `c >= m` is `true`, the program is ill-formed.

### Generator properties

The size of the states of `linear_congruential_engine` is `1`, each of them consists of a single integer.

The actual modulus m⁰ is defined as follows:

- If `m` is not zero, m⁰ is `m`.
- If `m` is zero, m⁰ is the value of `std::numeric_limits<result_type>::max()` plus `1` (which means m⁰ need not be representable as `result_type`).

The transition algorithm of `linear_congruential_engine` is TA(xⁱ) = (a·xⁱ+c) mod m⁰.

The generation algorithm of `linear_congruential_engine` is GA(xⁱ) = (a·xⁱ+c) mod m⁰.

The pseudo-random number generated with the current state is also the successor state.

### Predefined specializations

The following specializations define the random number engine with two commonly used parameter sets:

Defined in header `<random>`

**Type — Definition**

- `minstd_rand0` (C++11): `std::linear_congruential_engine<std::uint_fast32_t,` `16807, 0, 2147483647>` Discovered in 1969 by Lewis, Goodman and Miller, adopted as "Minimal standard" in 1988 by Park and Miller
- `minstd_rand` (C++11): `std::linear_congruential_engine<std::uint_fast32_t,` `48271, 0, 2147483647>` Newer "Minimum standard", recommended by Park, Miller, and Stockmeyer in 1993

### Nested types

**Type — Definition**

- `result_type`: `UIntType`

### Data members

- `constexpr UIntType multiplier` [static]: `a` (public static member constant)
- `constexpr UIntType increment` [static]: `c` (public static member constant)
- `constexpr UIntType modulus` [static]: `m` (public static member constant)
- `constexpr UIntType default_seed` [static]: `1u` (public static member constant)

### Member functions

**Construction and Seeding**

- (constructor): constructs the engine (public member function)
- `seed`: sets the current state of the engine (public member function)

**Generation**

- `operator()`: advances the engine's state and returns the generated value (public member function)
- `discard`: advances the engine's state by a specified amount (public member function)

**Characteristics**

- `min` [static]: gets the smallest possible value in the output range (public static member function)
- `max` [static]: gets the largest possible value in the output range (public static member function)

### Non-member functions

- `operator==` (C++11), `operator!=` (C++11)(removed in C++20): compares the internal states of two pseudo-random number engines (function)
- `operator<<` (C++11), `operator>>`: performs stream input and output on pseudo-random number engine (function template)

### Example

> Note: This section is incomplete Reason: no example

---
Attribution: content from https://en.cppreference.com/w/cpp/numeric/random/linear_congruential_engine (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.