Táto hlavička predstavuje možnosti generovania náhodných čísel. Táto knižnica umožňuje vytvárať náhodné čísla pomocou kombinácií generátorov a distribúcií.
- Distribúcie : Objekty, ktoré transformujú postupnosti čísel generované generátorom na postupnosti čísel, ktoré sledujú špecifické rozdelenie náhodných premenných, ako je napríklad rovnomerné normálne alebo binomické.
Generátory
I. Pseudonáhodné čísla: Používajú algoritmus na generovanie náhodných čísel na základe počiatočného zdroja. Toto sú:

1. linear_congruential_engine : Je to najjednoduchší nástroj v knižnici STL, ktorý generuje náhodné celé čísla bez znamienka. Z toho vyplýva:
plná forma i d e
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; }
výstup:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: Ide o nástroj s náhodnými číslami založený na algoritme Mersenne Twister. Vytvára vysoko kvalitné celočíselné náhodné čísla bez znamienka v intervale [0 (2^w)-1].
kde 'w' je veľkosť slova: Počet bitov každého slova v sekvencii stavov.
// 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; }
výstup:
3348201622 is a random number between 0 and 4294967295
3. subtract_with_carry_engine: Je to generátor pseudonáhodných čísel, ktorý vytvára celé čísla bez znamienka.
Použitý algoritmus je oneskorený Fibonacciho generátor so stavovou sekvenciou r celočíselných prvkov plus jedna prenosná hodnota.
// 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; }
výstup:
8606455 is a random number between 0 and 16777215
II. Generátor náhodných čísel : Je to generátor náhodných čísel, ktorý produkuje nedeterministické náhodné čísla.
// 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; }
výstup:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Pseudonáhodné čísla (inštancie) : Toto sú konkrétne príklady generátorových motorov a adaptérov:
ako zatvoriť režim vývojára

1. default_random_engine : Toto je trieda mechanizmu náhodných čísel, ktorá generuje pseudonáhodné čísla.
Funkcia zmení vnútorný stav o jeden, ktorý upraví hodnotu stavu podľa daného algoritmu:
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; }
výstup:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Generuje pseudonáhodné čísla; je to podobné lineárny kongruenciálny generátor
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; }
výstup:
matematické metódy v Jave
489592737 is a random number between 1 and 2147483646
3.MT19937: Je to generátor Mersenne Twister 19937. Ide o pseudonáhodný generátor 32-bitových čísel s veľkosťou stavu 19937 bitov.
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; }
výstup:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: Je to základný generátor Ranlux 24. Je to pseudonáhodný generátor subtract-with-carry 24-bitových čísel, ktorý sa všeobecne používa ako základný motor pre generátor ranlux24.
Funkcia zmení vnútorný stav volaním svojho prechodového algoritmu, ktorý na prvok aplikuje operáciu odčítania s prenášaním.
// 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; }
výstup:
7275352 is a random number between 0 and 16777215
Podobný formát je použiteľný aj pre ostatné príklady.
IV. Adaptéry motora

1. discard_block_engine: Je to šablóna triedy adaptéra motora, ktorá prispôsobuje a Generátor pseudonáhodných čísel Engine typu pomocou iba prvkov „r“ každého bloku prvkov „p“ zo sekvencie, ktorú vytvára, pričom zvyšok sa zahodí.
Adaptér uchováva interný počet vyrobených prvkov v aktuálnom bloku.
Štandardné generátory ranlux24 a ranlux48 prispôsobiť a odčítať_s_nosným_motorom pomocou tohto adaptéra.
// 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; }
výstup:
8132325 is a random number between 0 and 16777215
2. independent_bits_engine: Je to šablóna triedy adaptéra motora, ktorá prispôsobuje a Generátor pseudonáhodných čísel Engine zadajte náhodné čísla so špecifickým počtom bitov (w).
Algoritmus prechodu motora vyvolá člen operátora() základného motora toľkokrát, koľkokrát je potrebné na získanie dostatočného množstva významných bitov na vytvorenie náhodnej hodnoty.
// 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; }
výstup:
pole pridávanie prvkov java
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: Je to šablóna triedy adaptéra motora, ktorá prispôsobuje a Generátor pseudonáhodných čísel Engine typu, aby sa čísla doručili v inom poradí.
Objekt interne uchováva vyrovnávaciu pamäť k vygenerovaných čísel a na požiadanie vráti náhodne vybrané číslo vo vyrovnávacej pamäti, ktorá ho nahradí hodnotou získanou z jeho základného motora.
Algoritmus prechodu motora vyberie hodnotu z internej tabuľky (ktorú vráti funkcia) a nahradí ju novou hodnotou získanou z jeho základného motora.
// 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; }
výstup:
9213395 is a random number between 0 and 16777215Vytvoriť kvíz