The rand () function returns a pseudo-random number according to an UNIFORM distribution of random numbers. Uniform distribution than characterized? By the fact that, on average, according to the law of large numbers, these numbers will tend to a certain "average" value. rand () is just an implementation of the distribution law (and then, just a pseudo-random implementation). And could you clarify the question. Because you ask about rand (), but for some reason you do not use this function in the code. Instead, use the uniform_int_distribution class. In this case, for some reason, do not use the opportunity of this class: to set the minimum and maximum values of possible random (!) Numbers. for example
std::random_device rd; /*используется для получения начального числа для генератора случайных чисел*/ std::mt19937 gen(rd()); //стандартный mersenne_twister_engine с начальным числом rd() std::uniform_int_distribution<> dis(1, 6); /*тут мы устанавливаем минимальное и максимально возможные числа, т.е. диапазон случайных чисел*/ for (int n=0; n<10; ++n) /* можно использовать dis для преобразования случайного unsigned int, сгенерированного с помощью gen в int в диапазоне [1, 6] */ std::cout << dis(gen) << ' ';
Actually, the previous answer is practically the same, but as a function.
"Therefore, I cannot understand why for character numbers min () is set to 0, and not to the minimum value of the data type." - because it is the realization of a uniform distribution law. Look at the standard schedule of such a distribution law - and everything will immediately become clear to you.
Another example
#include <iostream> #include <random> #include <ctime> #include <string> int main() { std::mt19937 gen(time(0)); std::uniform_int_distribution<int> uid1(0, 50), uid2(uid1.param()); std::cout << "uid1 max: " << uid1.max() << std::endl << "uid1 min: " << uid1.min() << std::endl << "uid2 max: " << uid2.max() << std::endl << "uid2 min: " << uid2.min() << std::endl; }
The screen will display:
uid1 max: 50 uid1 min: 0 uid2 max: 50 uid2 min: 0