This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Generators”.

1. What are the Generators in C++?

a) An object that generates uniformly distributed numbers

b) An object that generates a number from a given sequence

c) An object that generates the smallest number from a given range

d) An object that generates unique numbers

View Answer

Explanation: Generators are objects that generates uniformly distributed numbers which help in generating random numbers.

2. What are Distributions in C++?

a) Objects that converts a sequence into a sequence having an ascending order

b) Objects that converts a sequence into a sequence having specific random variable distribution

c) Objects that converts a sequence into a sequence having a descending order

d) Objects that converts a sequence into a sequence having only the smallest, largest and median

View Answer

Explanation: Distributions are objects that convert the sequence generated by the generator into a sequence which has a specific random variable distribution like uniform, normal, binomial, etc.

3. Which header file is used for generating random numbers?

a) <gen_dist>

b) <distribution>

c) <generator>

d) <random>

View Answer

Explantion: <random> header file is required for using generators and distributions which helps in generating random numbers in a program.

4. What is Pseudo-random number engines?

a) Uses user input for random number generation

b) Uses an algorithm that does not require any initial seed to generate random numbers

c) Uses initial seed based algorithm to generate random numbers

d) Random number generates depends on the program

View Answer

Explanation: Pseudo-random number engines are used to generate random numbers based on an initial seed provided to them.

5. How many Pseudo-random number engines are there?

a) 1

b) 2

c) 3

d) 4

View Answer

Explanation: There are three types of Pseudo-random number engines based on the algorithm they use. They are linear_congruential_engine, mersenne_twister_engine and subtract_with_carry_engine.

6. What is linear_congruential_engine?

a) Pseudo-random number engine that generates random unsigned integers in the range [0, 2^{w-1}] for some w using Mersenne Twister algorithm

b) Pseudo-random number engine that generates random unsigned integers

c) Pseudo-random number engine that generates random unsigned integers in the range [0, 2^{w-1}] for some w using lagged Fibonacci generator

d) Pseudo-random number engine that generates random signed integers in the range [0, 2^{w-1}] for some w using Mersenne Twister algorithm

View Answer

Explanation: linear_congruential_engine is a simple Pseudo-random number engine that generates random unsigned integer using the basic algorithm.

7. What are different operations are used in Pseudo-random number engines?

a) operator()

b) min()

c) max()

d) all of the mentioned

View Answer

Explanation: Pseudo-random number engines use three operations operator() that generates a random number, min() that returns minimum value returned by member operator() and max() returns the maximum value.

8. What is mersenne_twister_engine?

a) Pseudo-random number engine that generates random unsigned integers

b) Pseudo-random number engine that generates random unsigned integers in the range [0, 2^{w-1}] for some w using Mersenne Twister algorithm

c) Pseudo-random number engine that generates random unsigned integers in the range [0, 2^{w-1}] for some w using lagged Fibonacci generator

d) Pseudo-random number engine that generates random signed integers in the range [0, 2^{w-1}] for some w using Mersenne Twister algorithm

View Answer

Explanation: mersenne_twister_engine is a Pseudo-random number engine that generates random unsigned integers in the range [0, 2

^{w-1}] for some w using Mersenne Twister algorithm.

9. What is subtract_with_carry_engine?

a) Pseudo-random number engine that generates random unsigned integers

b) Pseudo-random number engine that generates random unsigned integers in the range [0, 2^{w-1}] for some w using Mersenne Twister algorithm

c) Pseudo-random number engine that generates random unsigned integers in the range [0, 2^{w-1}] for some w using lagged Fibonacci generator

d) Pseudo-random number engine that generates random signed integers in the range [0, 2^{w-1}] for some w using Mersenne Twister algorithm

View Answer

Explanation: subtract_with_carry_engine is a Pseudo-random number engine that generates random unsigned integers in the range [0, 2

^{w-1}] for some w using lagged Fibonacci generator.

10. What is a Random number generator?

a) A generator that generates deterministic random numbers

b) A generator that generates both non-deterministic random numbers and deterministic random numbers

c) A generator that generates non-deterministic random numbers

d) A generator that generates a simple random number

View Answer

Explanation: Random number generator is a random number generator which generates non-deterministic random numbers.

11. What is random_device?

a) A pseudo-random number generator

b) Time-dependent random number generator

c) Simple random number generator

d) A true random number generator

View Answer

Explanation: random_device is a true random number generator not the pseudo random number generator.

12. Which algorithm is used in subtract_with_carry_engine?

a) Mersenne Twister algorithm

b) Lagged fibonacci generator algorithm

c) Either the Mersenne Twister algorithm or Lagged fibonacci generator algorithm

d) Fibonacci generator

View Answer

Explanation: The algorithm used in subtract_with_carry_engine is lagged fibonacci generator, with state sequences.

13. What is the default random engine?

a) Random number engine that generates pseudo-random numbers

b) Mersenne Twister 19937 generator generating 32-bit true random number

c) Random number engine that generates true random numbers

d) Mersenne Twister 19937 generator generating 32-bit pseudo random number

View Answer

Explanation: Default random engine is a random number engine that generates pseudo random numbers.

14. What are Engine Adaptors?

a) Class template that adopts a pseudo-random number generator engine

b) Class template that adopts a pseudo-random number generator engine to produce numbers with a given numbers of bits

c) Random number engine that generates pseudo-random numbers

d) Mersenne Twister 19937 generator generating 32-bit true random number

View Answer

Explanation: Engine adaptor is a class template that adapts a pseudo-random number generator to produce random number having a specific number of bits.

15. How many Engine Adaptors are there in C++?

a) 1

b) 2

c) 3

d) 4

View Answer

Explanation: There are three types of Engine adaptors in C++, namely discard_block_engine, independent_bits_engine and shuffle_order_engine.

**Sanfoundry Global Education & Learning Series – C++ Programming Language.**