rng c++
Certainly! Here is a response:
#include <iostream>
#include <random>
int main() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 100);
int random_number = dis(gen);
std::cout << "Random number: " << random_number << std::endl;
return 0;
}
Explanation:
- #include <iostream>
: This line includes the input/output stream library, which allows us to perform input and output operations.
- #include <random>
: This line includes the random number generation library, which provides facilities to generate random numbers.
- std::random_device rd;
: This line creates a random_device object, which serves as a source of non-deterministic random numbers.
- std::mt19937 gen(rd());
: This line creates a Mersenne Twister pseudo-random number generator engine, seeded with the random_device object.
- std::uniform_int_distribution<> dis(1, 100);
: This line creates a uniform distribution of integers between 1 and 100.
- int random_number = dis(gen);
: This line generates a random number using the distribution and the random number generator engine.
- std::cout << "Random number: " << random_number << std::endl;
: This line outputs the generated random number to the console.
- return 0;
: This line indicates the successful termination of the program.