Questa intestazione introduce le funzionalità di generazione di numeri casuali. Questa libreria consente di produrre numeri casuali utilizzando combinazioni di generatori e distribuzioni.
- Distribuzioni : Oggetti che trasformano sequenze di numeri generate da un generatore in sequenze di numeri che seguono una specifica distribuzione di variabili casuali come Normale uniforme o Binomiale.
Generatori
I. Motori con numeri pseudo-casuali: Usano un algoritmo per generare numeri casuali basati su un seme iniziale. Questi sono:

1. motore_lineare_congruenziale : È il motore più semplice nella libreria STL che genera numeri interi casuali senza segno. Ne consegue:
stringa inversa in Java
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; }
Produzione:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: È un motore di numeri casuali basato sull'algoritmo Mersenne Twister. Produce numeri casuali interi senza segno di alta qualità nell'intervallo [0 (2^w)-1].
dove 'w' è la dimensione della parola: numero di bit di ciascuna parola nella sequenza di stati.
// 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; }
Produzione:
3348201622 is a random number between 0 and 4294967295
3. sottrai_con_trasporta_motore: È un motore di generazione di numeri pseudo-casuali che produce numeri interi senza segno.
L'algoritmo utilizzato è ritardato generatore di Fibonacci con una sequenza di stati di r elementi interi più un valore di riporto.
come ottenere una data corrente in Java
// 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; }
Produzione:
8606455 is a random number between 0 and 16777215
II. Generatore di numeri casuali : È un generatore di numeri casuali che produce numeri casuali non deterministici.
// 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; }
Produzione:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Motori con numeri pseudo-casuali (istanziazioni) : Queste sono le istanziazioni particolari dei motori generatori e degli adattatori:

1. default_random_engine : Questa è una classe di motori di numeri casuali che genera numeri pseudo-casuali.
La funzione cambia lo stato interno con uno che modifica il valore dello stato in base all'algoritmo fornito:
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; }
Produzione:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Genera numeri pseudo casuali; è simile a generatore congruenziale lineare
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; }
Produzione:
489592737 is a random number between 1 and 2147483646
3.MT19937: È il generatore Mersenne Twister 19937. È un generatore pseudo-casuale di numeri a 32 bit con una dimensione dello stato di 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; }
Produzione:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: È un generatore di base Ranlux 24. È un generatore pseudo-casuale di sottrazione con riporto di numeri a 24 bit generalmente utilizzato come motore di base per il generatore ranlux24.
La funzione modifica lo stato interno chiamando il suo algoritmo di transizione che applica un'operazione di sottrazione con riporto sull'elemento.
// 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; }
Produzione:
7275352 is a random number between 0 and 16777215
Un formato simile è applicabile agli altri esempi.
IV. Adattatori motore
differenza tra albero binario e albero di ricerca binario

1. scartare_blocco_motore: È un modello di classe dell'adattatore del motore che adatta a Motore generatore di numeri pseudo-casuali digitare utilizzando solo gli elementi 'r' di ciascun blocco di elementi 'p' della sequenza che produce scartando il resto.
L'adattatore mantiene un conteggio interno di quanti elementi sono stati prodotti nel blocco corrente.
transizione dell'opacità css
I generatori standard ranlux24 E ranlux48 adattare a sottrai_con_trasporta_motore utilizzando questo adattatore.
// 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; }
Produzione:
8132325 is a random number between 0 and 16777215
2. motore_bit_indipendente: È un modello di classe dell'adattatore del motore che adatta a Motore generatore di numeri pseudo-casuali digitare per produrre numeri casuali con un numero specifico di bit (w).
L'algoritmo di transizione del motore richiama il membro operator() dei motori di base tutte le volte necessarie per ottenere bit significativi sufficienti per costruire un valore casuale.
// 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; }
Produzione:
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: È un modello di classe dell'adattatore del motore che adatta a Motore generatore di numeri pseudo-casuali digitare in modo che i numeri vengano consegnati in una sequenza diversa.
L'oggetto mantiene internamente un buffer di k numeri generati e quando richiesto restituisce un numero selezionato casualmente all'interno del buffer sostituendolo con un valore ottenuto dal suo motore di base.
L'algoritmo di transizione del motore seleziona un valore nella tabella interna (che viene restituita dalla funzione) e lo sostituisce con un nuovo valore ottenuto dal suo motore di base.
// 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; }
Produzione:
9213395 is a random number between 0 and 16777215Crea quiz