Ovo zaglavlje uvodi mogućnost generiranja nasumičnog broja. Ova biblioteka omogućuje proizvodnju nasumičnih brojeva korištenjem kombinacija generatora i distribucija.
- Distribucije : Objekti koji pretvaraju nizove brojeva koje generira generator u nizove brojeva koji slijede određenu distribuciju slučajnih varijabli kao što je uniformna normalna ili binomna.
Generatori
I. Motori pseudoslučajnih brojeva: Oni koriste algoritam za generiranje nasumičnih brojeva na temelju početnog sjemena. Ovo su:

1. linearni_kongruencijalni_motor : To je najjednostavniji mehanizam u STL biblioteci koji generira nasumične nepredznačene cijele brojeve. Slijedi:
cdr puni oblik
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; }
Izlaz:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_motor: To je mehanizam za odabir slučajnih brojeva temeljen na algoritmu Mersenne Twister. Proizvodi visokokvalitetne nepredznačene cijele slučajne brojeve u intervalu [0 (2^w)-1].
gdje je 'w' veličina riječi: broj bitova svake riječi u nizu 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; }
Izlaz:
3348201622 is a random number between 0 and 4294967295
3. oduzmi_sa_prenosnim_motorom: To je mehanizam za generiranje pseudoslučajnih brojeva koji proizvodi cijele brojeve bez predznaka.
Algoritam koji se koristi je zaostao fibonaccijev generator s nizom stanja od r cjelobrojnih elemenata plus jedna prijenosna vrijednost.
// 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; }
Izlaz:
8606455 is a random number between 0 and 16777215
II. Generator slučajnih brojeva : To je generator slučajnih brojeva koji proizvodi nedeterminističke slučajne brojeve.
puni oblik ide
// 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; }
Izlaz:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Motori za pseudoslučajne brojeve (instancije) : Ovo su konkretne instance generatorskih motora i adaptera:

1. default_random_engine : Ovo je klasa mehanizma za nasumične brojeve koja generira pseudo-nasumične brojeve.
Funkcija mijenja interno stanje s onom koja modificira vrijednost stanja prema zadanom 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; }
Izlaz:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Generira pseudoslučajne brojeve; slično je linearni kongruencijalni 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; }
Izlaz:
489592737 is a random number between 1 and 2147483646
3.MT19937: To je Mersenne Twister 19937 generator. To je pseudo-slučajni generator 32-bitnih brojeva s veličinom stanja od 19937 bita.
kako dereferencirati pokazivač u c
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; }
Izlaz:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: To je Ranlux 24 base generator. To je pseudoslučajni generator oduzimanja s prijenosom 24-bitnih brojeva koji se općenito koristi kao osnovni mehanizam za generator ranlux24.
Funkcija mijenja unutarnje stanje pozivanjem svog algoritma prijelaza koji primjenjuje operaciju oduzimanja s prijenosom na element.
// 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; }
Izlaz:
7275352 is a random number between 0 and 16777215
Sličan format primjenjiv je i za ostale primjere.
IV. Adapteri motora

1. discard_block_engine: To je predložak klase adaptera motora koji prilagođava a generator pseudoslučajnih brojeva Engine tip koristeći samo 'r' elemente svakog bloka 'p' elemenata iz niza koji proizvodi odbacujući ostatak.
Adapter interno broji koliko je elemenata proizvedeno u trenutnom bloku.
Standardni generatori ranlux24 i ranlux48 prilagoditi a oduzmi_sa_prenosnim_motorom pomoću ovog adaptera.
// 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; }
Izlaz:
kako provjeriti veličinu zaslona monitora
8132325 is a random number between 0 and 16777215
2. neovisni_bitni_motor: To je predložak klase adaptera motora koji prilagođava a generator pseudoslučajnih brojeva Engine tip za proizvodnju nasumičnih brojeva s određenim brojem bitova (w).
Algoritam prijelaza motora poziva člana operator() osnovnog motora onoliko puta koliko je potrebno da dobije dovoljno značajnih bitova za konstrukciju slučajne vrijednosti.
// 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; }
Izlaz:
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: To je predložak klase adaptera motora koji prilagođava a generator pseudoslučajnih brojeva Engine upišite tako da se brojevi isporučuju u drugom nizu.
Objekt interno čuva međuspremnik od k generiranih brojeva i na zahtjev vraća nasumično odabrani broj unutar međuspremnika zamjenjujući ga vrijednošću dobivenom iz svog osnovnog stroja.
Algoritam prijelaza motora odabire vrijednost u internoj tablici (koju vraća funkcija) i zamjenjuje je novom vrijednošću dobivenom iz njegovog osnovnog 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; }
Izlaz:
9213395 is a random number between 0 and 16777215Napravi kviz