Тази заглавка въвежда съоръжения за генериране на произволни числа. Тази библиотека позволява да се произвеждат произволни числа, като се използват комбинации от генератори и разпределения.
- Разпределения : Обекти, които трансформират поредици от числа, генерирани от генератор, в поредици от числа, които следват специфично разпределение на произволна променлива, като например равномерно нормално или биномно.
Генератори
I. Механизми за псевдослучайни числа: Те използват алгоритъм за генериране на произволни числа въз основа на първоначално семе. Това са:

1. линеен_конгруентен_двигател : Това е най-простият двигател в библиотеката STL, който генерира произволни цели числа без знак. Следва:
размери на шрифта в латекс
x = (a.x +c) mod m Where x= current state value a = multiplier parameter ; if m is not zero this parameter should be lower than m. c = increment parameter ; if m is not zero this parameter should be lower than m. m = modulus parameter
// C++ program to illustrate // the use of operator() max and min // in linear_congruential_engine #include #include #include using namespace std; // driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // minstd_rand0 is a standard // linear_congruential_engine minstd_rand0 generator (seed); // generates the random number cout << generator() << ' is a random number between '; //use of min and max functions cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: Това е машина за произволни числа, базирана на алгоритъма на Mersenne Twister. Той произвежда висококачествени произволни цели числа без знак в интервала [0 (2^w)-1].
където 'w' е размер на думата: Брой битове на всяка дума в последователността на състоянието.
// C++ program to illustrate the use of // operator() min and max // in mersenne_twister_engine #include #include #include using namespace std; // Driver program int main () { // finds the time between the system clock // (present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // mt19937 is a standard mersenne_twister_engine mt19937 generator (seed); // use of operator() cout << generator() << ' is a random number between '; // use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
3348201622 is a random number between 0 and 4294967295
3. subtract_with_carry_engine: Това е машина за генератор на псевдослучайни числа, която произвежда цели числа без знак.
Използваният алгоритъм е лаг генератор на фибоначи с последователност от състояния от r цели числа плюс една пренасяща стойност.
какво е експорт в linux
// C++ program to illustrate the use of // operator() min and max // in subtract_with_carry_engine #include #include #include using namespace std; // Driver program int main () { // finds the time between the system clock // (present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); subtract_with_carry_engine<unsigned 24 10 24> generator (seed); // use of operator() cout << generator() << ' is a random number between '; // use of min and max cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
8606455 is a random number between 0 and 16777215
II. Генератор на случайни числа : Това е генератор на случайни числа, който произвежда недетерминирани произволни числа.
// C++ program to illustrate the use of // operator() min and max // in random_device #include #include using namespace std; //Driver program int main () { random_device example; cout << 'default random_device characteristics:' << endl; // use of min cout << 'minimum: ' << example.min() << endl; // use of max cout << 'maximum: ' << example.max() << endl; // use of entropy cout << 'entropy: ' << example.entropy() << endl; // use of operator() cout << 'a random number: ' << example() << endl; return 0; }
Изход:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Механизми за псевдослучайни числа (инстанции) : Това са конкретните екземпляри на генераторни двигатели и адаптери:

1. default_random_engine : Това е клас машина за произволни числа, която генерира псевдослучайни числа.
Функцията променя вътрешното състояние с едно, което променя стойността на състоянието според зададения алгоритъм:
x= (a.x + c)mod m Where x= current state value a and c = respective class template parameters m = class template parameterC++
// C++ program to illustrate the use of // operator() min and max // in default_random_engine #include #include #include using namespace std; // Driver program int main () { // finds the time between the system clock // (present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // minstd_rand0 is a standard linear_congruential_engine minstd_rand0 generator (seed); // generates the random number cout << generator() << ' is a random number between '; // Use of min and max cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Той генерира псевдослучайни числа; то е подобно на линеен конгруентен генератор
x = (a.x + c) mod m where x= current state value a c and m=class template parameter
// C++ program to illustrate // the use of operator() max and min // in minstd_rand #include #include #include using namespace std; //Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // minstd_rand0 is a standard //linear_congruential_engine minstd_rand0 generator (seed); // use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
489592737 is a random number between 1 and 2147483646
3.MT19937: Това е генератор на Mersenne Twister 19937. Това е псевдослучаен генератор на 32-битови числа с размер на състоянието от 19937 бита.
C++
// C++ program to illustrate the // use of operator()min and max // in mt19937 #include #include #include using namespace std; // Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // mt19937 is a standard //mersenne_twister_engine mt19937 generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: Това е базов генератор Ranlux 24. Това е псевдопроизволен генератор на 24-битови числа с изваждане с пренасяне, който обикновено се използва като базов двигател за генератора ranlux24.
Функцията променя вътрешното състояние, като извиква своя алгоритъм за преход, който прилага операция за изваждане с пренасяне върху елемента.
// C++ program to illustrate // the use of operator()min and max // in ranlux24_base #include #include #include using namespace std; //Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); subtract_with_carry_engine<unsigned241024> generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
7275352 is a random number between 0 and 16777215
Подобен формат е приложим за другите примери.
IV. Адаптери за двигател
пример за java do while

1. discard_block_engine: Това е шаблон за клас адаптер на двигател, който адаптира a Генератор на псевдослучайни числа тип, като използва само 'r' елементи от всеки блок от 'p' елементи от последователността, която произвежда, като изхвърля останалите.
Адаптерът поддържа вътрешен брой на броя на произведените елементи в текущия блок.
низ към jsonobject
Стандартните генератори ranlux24 и ranlux48 адаптирам a изваждане_с_двигател_за_пренасяне използвайки този адаптер.
// C++ program to illustrate // the use of operator()min and max // in the discard_block_engine #include #include #include using namespace std; //Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // ranlux24 is a standard instantiation //of discard_block_engine: ranlux24 generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
8132325 is a random number between 0 and 16777215
2. независим_битов_двигател: Това е шаблон за клас адаптер на двигател, който адаптира a Генератор на псевдослучайни числа тип за създаване на произволни числа с определен брой битове (w).
Алгоритъмът за преход на двигателя извиква члена operator() на базовите двигатели толкова пъти, колкото е необходимо, за да получи достатъчно значими битове за конструиране на произволна стойност.
// C++ program to illustrate // the use of operator()min and max // in independent_bits_engine #include #include // It imports the symbol names in // std namespace and possibly in Global namespace. #include #include using namespace std; //Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); //use of independent_bits_engine independent_bits_engine<mt1993764uint_fast64_t> generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: Това е шаблон за клас адаптер на двигател, който адаптира a Генератор на псевдослучайни числа въведете, така че числата да се доставят в различна последователност.
Обектът съхранява буфер от k генерирани числа вътрешно и при поискване връща произволно избрано число в рамките на буфера, като го замества със стойност, получена от основния двигател.
Алгоритъмът за преход на двигателя избира стойност във вътрешната таблица (която се връща от функцията) и я заменя с нова стойност, получена от основния двигател.
// C++ program to illustrate // the use of operator()min and max // in shuffle_order_engine #include #include #include using namespace std; int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // ranlux24 is a standard instantiation // of discard_block_engine: ranlux24 generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
Изход:
9213395 is a random number between 0 and 16777215Създаване на тест