Ta glava uvaja zmogljivosti za generiranje naključnih števil. Ta knjižnica omogoča ustvarjanje naključnih števil z uporabo kombinacij generatorjev in distribucij.
- Distribucije : Objekti, ki pretvarjajo zaporedja števil, ki jih ustvari generator, v zaporedja števil, ki sledijo določeni porazdelitvi naključnih spremenljivk, kot je enotna normalna ali binomska.
Generatorji
I. Motorji psevdonaključnih števil: Uporabljajo algoritem za ustvarjanje naključnih števil na podlagi začetnega semena. To so:

1. linearni_skladni_motor : Je najpreprostejši mehanizem v knjižnici STL, ki generira naključna cela števila brez predznaka. Sledi:
prednosti in slabosti tehnologije
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; }
Izhod:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: Je mehanizem naključnih števil, ki temelji na algoritmu Mersenne Twister. Proizvaja visokokakovostna naključna cela števila brez predznaka v intervalu [0 (2^w)-1].
kjer je 'w' velikost besede: število bitov vsake besede v zaporedju stanja.
// 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; }
Izhod:
3348201622 is a random number between 0 and 4294967295
3. subtract_with_carry_engine: Je generator psevdonaključnih števil, ki proizvaja nepredznačena cela števila.
Uporabljeni algoritem je zaostal fibonaccijev generator z zaporedjem stanj r celih elementov plus ena prenosna vrednost.
// 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; }
Izhod:
8606455 is a random number between 0 and 16777215
II. Generator naključnih števil : Je generator naključnih števil, ki proizvaja nedeterministična naključna števila.
// 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; }
Izhod:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Motorji psevdonaključnih števil (instancije) : To so posebne izvedbe generatorskih motorjev in adapterjev:
funkcija python chr

1. privzeti_naključni_motor : To je razred mehanizma za naključna števila, ki ustvarja psevdonaključna števila.
Funkcija spremeni notranje stanje z eno, ki spremeni vrednost stanja v skladu z danim algoritmom:
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; }
Izhod:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Generira psevdo naključna števila; podobno je linearni kongruenčni generator
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; }
Izhod:
489592737 is a random number between 1 and 2147483646
3.MT19937: Je generator Mersenne Twister 19937. Je psevdo-naključni generator 32-bitnih števil z velikostjo stanja 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; }
Izhod:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: To je osnovni generator Ranlux 24. To je psevdonaključni generator 24-bitnih števil z odštevanjem s prenosom, ki se običajno uporablja kot osnovni motor za generator ranlux24.
Funkcija spremeni notranje stanje tako, da pokliče svoj prehodni algoritem, ki za element uporabi operacijo odštevanja s prenosom.
// 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; }
Izhod:
koliko je stara kylie jenner
7275352 is a random number between 0 and 16777215
Podobna oblika velja za druge primere.
IV. Adapterji motorja

1. discard_block_engine: Je predloga razreda adapterja motorja, ki prilagaja a Generator psevdonaključnih števil tipa z uporabo samo elementov 'r' vsakega bloka elementov 'p' iz zaporedja, ki ga proizvede, pri čemer zavrže ostale.
Adapter vodi notranje štetje, koliko elementov je bilo proizvedenih v trenutnem bloku.
Standardni generatorji ranlux24 in ranlux48 prilagoditi a odštej_z_prenosnim_motorjem uporabo tega adapterja.
// 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; }
Izhod:
8132325 is a random number between 0 and 16777215
2. neodvisni_bitni_motor: Je predloga razreda adapterja motorja, ki prilagaja a Generator psevdonaključnih števil tip za izdelavo naključnih števil z določenim številom bitov (w).
Prehodni algoritem motorja prikliče člana operator() osnovnega motorja tolikokrat, kot je potrebno, da pridobi dovolj pomembnih bitov za sestavo naključne vrednosti.
// 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; }
Izhod:
java string append
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: Je predloga razreda adapterja motorja, ki prilagaja a Generator psevdonaključnih števil tip, tako da so številke dostavljene v drugačnem zaporedju.
Objekt hrani medpomnilnik k ustvarjenih števil interno in na zahtevo vrne naključno izbrano število znotraj medpomnilnika, ki ga nadomesti z vrednostjo, pridobljeno iz njegovega osnovnega mehanizma.
Prehodni algoritem motorja izbere vrednost v notranji tabeli (ki jo vrne funkcija) in jo nadomesti z novo vrednostjo, pridobljeno iz osnovnega motorja.
// 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; }
Izhod:
9213395 is a random number between 0 and 16777215Ustvari kviz