📅  最后修改于: 2023-12-03 15:34:34.185000             🧑  作者: Mango
QRandomGenerator Bounded is a class provided by the Qt Framework that generates random numbers within a specified range. It is part of the QtCore module and can be used in any C++ project with Qt integration.
To use QRandomGenerator Bounded, first create an instance of the class by calling the constructor. Then call the bounded
method with the lower and upper bounds of the desired range as parameters.
#include <QRandomGenerator>
QRandomGenerator rand_gen; // create instance
int random_number = rand_gen.bounded(1, 10); // generate number between 1 and 10
The bounded
function returns a random number within the range specified, including both the lower and upper bounds.
By default, QRandomGenerator Bounded uses a system-generated seed value for generating random numbers. However, you can also use a custom seed and generate predictable sequences of random numbers.
#include <QRandomGenerator>
QRandomGenerator rand_gen(42); // create instance with custom seed
int random_number = rand_gen.bounded(1, 10); // generates the same random number every time
QRandomGenerator Bounded is a simple and efficient way to generate random numbers within a specified range for any C++ project with Qt integration. It is versatile enough to generate predictable sequences of random numbers or generate random numbers with a system-generated seed value.