Tato hlavička představuje zařízení pro generování náhodných čísel. Tato knihovna umožňuje vytvářet náhodná čísla pomocí kombinací generátorů a distribucí.
- Distribuce : Objekty, které transformují posloupnosti čísel generované generátorem na posloupnosti čísel, které sledují specifické rozdělení náhodných proměnných, jako je uniformní normální nebo binomické.
Generátory
I. Pseudonáhodné číslo: Používají algoritmus ke generování náhodných čísel na základě počátečního semene. Jedná se o:

1. linear_congruential_engine : Je to nejjednodušší engine v knihovně STL, který generuje náhodná celá čísla bez znaménka. Následuje:
jak vybrat sloupce z různých tabulek v sql
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: Je to stroj s náhodnými čísly založený na algoritmu Mersenne Twister. Vytváří vysoce kvalitní celočíselná náhodná čísla bez znaménka v intervalu [0 (2^w)-1].
kde 'w' je velikost slova: Počet bitů každého slova ve stavové sekvenci.
// 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. odečíst_s_přenosným_motorem: Je to generátor pseudonáhodných čísel, který produkuje celá čísla bez znaménka.
Použitý algoritmus je opožděný Fibonacciho generátor se stavovou sekvencí r celočíselných prvků plus jedna nosná 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, který 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é číslo stroje (instanciace) : Toto jsou konkrétní instance generátorových motorů a adaptérů:

1. default_random_engine : Toto je třída generátoru náhodných čísel, která generuje pseudonáhodná čísla.
python seřazená n-tice
Funkce změní vnitřní stav o jedničku, která upraví hodnotu stavu podle 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ární kongruenciální 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:
489592737 is a random number between 1 and 2147483646
3.MT19937: Je to generátor Mersenne Twister 19937. Jde o pseudonáhodný generátor 32bitových čísel s velikostí stavu 19937 bitů.
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 24bitových čísel obecně používaný jako základní engine pro generátor ranlux24.
jtlačítko
Funkce změní vnitřní stav voláním svého přechodového algoritmu, který na prvek aplikuje operaci odečítání s přenášení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žitelný pro další příklady.
IV. Adaptéry motoru

1. discard_block_engine: Je to šablona třídy adaptéru motoru, která přizpůsobuje a Generátor pseudonáhodných čísel Engine zadejte pouze pomocí prvků 'r' každého bloku prvků 'p' ze sekvence, kterou produkuje, přičemž zbytek se zahazuje.
Adaptér udržuje interní počet, kolik prvků bylo vyrobeno v aktuálním bloku.
Standardní generátory ranlux24 a ranlux48 přizpůsobit a odečíst_s_nosným_motorem pomocí tohoto adaptéru.
// 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. nezávislý_bitový_engine: Je to šablona třídy adaptéru motoru, která přizpůsobuje a Generátor pseudonáhodných čísel Engine zadejte pro vytváření náhodných čísel se specifickým počtem bitů (w).
Algoritmus přechodu enginu vyvolá člen operátoru() základního enginu tolikrát, kolikrát je potřeba, aby získal dostatek významných bitů pro vytvoření náhodné 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:
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: Je to šablona třídy adaptéru motoru, která přizpůsobuje a Generátor pseudonáhodných čísel Engine typu, aby se čísla doručovala v jiném pořadí.
Objekt interně uchovává vyrovnávací paměť k generovaných čísel a na požádání vrátí náhodně vybrané číslo ve vyrovnávací paměti a nahradí jej hodnotou získanou z jeho základního enginu.
Algoritmus přechodu enginu vybere hodnotu v interní tabulce (kterou vrátí funkce) a nahradí ji novou hodnotou získanou z jeho základního enginu.
// 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:
kde jsou nastavení prohlížeče
9213395 is a random number between 0 and 16777215Vytvořit kvíz