Javatpoint Logo
Javatpoint Logo

std::subtract_with_carry_engine in C++

In this article, you will learn about the std::substract_with_carry_engine in C++ with its syntax, parameters, and examples.

What is the std::subtract_with_carry_engine?

The C++ template class std::subtract_with_carry_engine implements a subtract-with-carry random number engine. This engine is defined in the <random> header and is included in the C++ Standard Library.


It is a quick summary of the template for the std::subtract_with_carry_engine class:

  • UIntType: The unsigned integer type that will be utilized to represent the engine's internal state. This type must meet the requirements of the UniformRandomBitGenerator
  • w: The quantity of bits contained in a state word. It establishes how big the internal state is.
  • s: The distance subtracted affects how a carry behaves.
  • "r": The word count within the state array.

The subtract-with-carry engine produces pseudo-random numbers by repeatedly subtracting a carry term from the sum of two internal state words. The engine is reversible because it makes use of the subtraction operation.


An example of using the std::subtract_with_carry_engine is as follows:


std::subtract_with_carry_engine in C++


  • In this example, the subtract_with_carry_engine is instantiated with 48 bits, a subtraction distance of 5, a 64-bit unsigned integer type, and ten words in the state array. After that, the engine produces five random numbers.
  • Remember that the template parameters you supply determine the engine's specific features. The quality and duration of the generated sequence are affected by the values for w, s, and r. These parameters should be selected based on the desired characteristics of the random number generator for your particular use case.

Some Functions:

A few member functions of the std::subtract_with_carry_engine carry out the common operations:

S. No. Function Description
1 min() It is used to determine the lowest value that can be produced.
2 max() It is utilized to determine the highest value that can be produced.
3 seed() The process of generating random numbers uses the seed.
4 operator () The operator () is overloaded to return the randomly generated number.

Example 1:

Let us take an example to illustrate the std::subtract_with_carry_engine in C++.


std::subtract_with_carry_engine in C++

Example 2:

Let us take another example to illustrate the std::subtract_with_carry_engine in C++.


std::subtract_with_carry_engine in C++

Benefits of std::subtract_with_carry_engine:

The C++ Standard Library offers a random number engine called std::subtract_with_carry_engine. It is meant to produce random numbers using a subtract-with-carry algorithm and is a <random> header component. These are a few advantages of utilizing this engine:

  1. Higher Random Numbers: It is well-known that the subtract-with-carry algorithm generates superior random numbers. Given its strong statistical properties, the generated numbers are more likely to be uniformly distributed and display other desired attributes.
  2. Period Length: The amount of random values the subtract-with-carry engine can produce before it begins repeating the sequence is known as its period length and is usually quite long. In general, a longer duration is considered preferable for some applications to prevent repetition and predictability.
  3. Reproducibility: It is possible to set and retrieve the internal state of the engine. It implies that using a particular seed will enable you to generate random numbers in a repeatable manner. Applications where you require the same random number sequence for testing or debugging may find this helpful.
  4. Flexibility: The C++ library is made to be both adaptable and scalable. It offers a standard interface for different generators, distributions, and random number engines. It makes switching between engines or distributions simple without affecting how your code is organized overall.
  5. Uniformity Throughout Platforms: Code utilizing std::subtract_with_carry_engine is probably more platform and compiler-portable because it is a component of the C++ Standard Library. It can be beneficial when developing cross-platform applications.

It is important to remember that the particular requirements of your application will determine which random number generator you use. Different algorithms might work better in different situations, and the C++ Standard Library offers more options than just the subtract-with-carry engine. Depending on your needs, you may also want to investigate other engines, like std::linear_congruential_engine or std::mersenne_twister_engine.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA