Cet en-tête introduit des fonctionnalités de génération de nombres aléatoires. Cette bibliothèque permet de produire des nombres aléatoires en utilisant des combinaisons de générateurs et de distributions.
- Distribution : Objets qui transforment des séquences de nombres générées par un générateur en séquences de nombres qui suivent une distribution de variables aléatoires spécifique telle que Normale uniforme ou Binomiale.
Générateurs
I. Moteurs de nombres pseudo-aléatoires : Ils utilisent un algorithme pour générer des nombres aléatoires basés sur une graine initiale. Ce sont :

1. moteur_congruentiel_linéaire : C'est le moteur le plus simple de la bibliothèque STL qui génère des nombres entiers aléatoires non signés. Il s'ensuit :
comment obtenir la date actuelle en 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; }
Sortir:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: Il s'agit d'un moteur de nombres aléatoires basé sur l'algorithme Mersenne Twister. Il produit des nombres aléatoires entiers non signés de haute qualité dans l'intervalle [0 (2 ^ w) -1].
où « w » est la taille du mot : nombre de bits de chaque mot dans la séquence d'états.
// 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; }
Sortir:
3348201622 is a random number between 0 and 4294967295
3. subtract_with_carry_engine : Il s'agit d'un moteur générateur de nombres pseudo-aléatoires qui produit des nombres entiers non signés.
L'algorithme utilisé est un retard générateur de Fibonacci avec une séquence d'états de r éléments entiers plus une valeur de retenue.
// 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; }
Sortir:
8606455 is a random number between 0 and 16777215
II. Générateur de nombres aléatoires : C'est un générateur de nombres aléatoires qui produit des nombres aléatoires non déterministes.
// 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; }
Sortir:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Moteurs de nombres pseudo-aléatoires (instanciations) : Ce sont les instanciations particulières des moteurs générateurs et des adaptateurs :
kylie jenner âge

1. moteur_random_par défaut : Il s'agit d'une classe de moteur de nombres aléatoires qui génère des nombres pseudo-aléatoires.
La fonction change l'état interne par un qui modifie la valeur de l'état selon l'algorithme donné :
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; }
Sortir:
201066682 is a random number between 1 and 2147483646
2. minstd_rand : Il génère des nombres pseudo-aléatoires ; c'est semblable à générateur congruentiel linéaire
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; }
Sortir:
sauter la liste
489592737 is a random number between 1 and 2147483646
3.MT19937 : Il s'agit du générateur Mersenne Twister 19937. C'est un générateur pseudo-aléatoire de nombres 32 bits avec une taille d'état de 19937 bits.
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; }
Sortir:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base : Il s'agit du générateur de base Ranlux 24. Il s'agit d'un générateur pseudo-aléatoire de soustraction avec report de nombres 24 bits généralement utilisé comme moteur de base pour le générateur ranlux24.
La fonction modifie l'état interne en appelant son algorithme de transition qui applique une opération de soustraction avec report sur l'élément.
// 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; }
Sortir:
7275352 is a random number between 0 and 16777215
Un format similaire est applicable pour les autres exemples.
IV. Adaptateurs moteur

1. throw_block_engine : Il s'agit d'un modèle de classe d'adaptateur de moteur qui adapte un générateur de nombres pseudo-aléatoires tapez en utilisant uniquement les éléments « r » de chaque bloc d’éléments « p » de la séquence qu’il produit en supprimant le reste.
L'adaptateur conserve un décompte interne du nombre d'éléments qui ont été produits dans le bloc actuel.
Les générateurs standards ranlux24 et ranlux48 adapter un soustract_with_carry_engine en utilisant cet adaptateur.
// 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; }
Sortir:
8132325 is a random number between 0 and 16777215
2. moteur_bits_indépendant : Il s'agit d'un modèle de classe d'adaptateur de moteur qui adapte un générateur de nombres pseudo-aléatoires tapez pour produire des nombres aléatoires avec un nombre spécifique de bits (w).
L'algorithme de transition du moteur appelle le membre Operator() des moteurs de base autant de fois que nécessaire pour obtenir suffisamment de bits significatifs pour construire une valeur aléatoire.
// 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; }
Sortir:
rdbms
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine : Il s'agit d'un modèle de classe d'adaptateur de moteur qui adapte un générateur de nombres pseudo-aléatoires tapez pour que les nombres soient livrés dans un ordre différent.
L'objet conserve un tampon de k nombres générés en interne et, lorsqu'il est demandé, renvoie un nombre sélectionné au hasard dans le tampon en le remplaçant par une valeur obtenue à partir de son moteur de base.
L'algorithme de transition du moteur sélectionne une valeur dans la table interne (qui est renvoyée par la fonction) et la remplace par une nouvelle valeur obtenue à partir de son moteur de 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; }
Sortir:
9213395 is a random number between 0 and 16777215Créer un quiz