Este encabezado presenta funciones de generación de números aleatorios. Esta biblioteca permite producir números aleatorios utilizando combinaciones de generadores y distribuciones.
- Distribuciones : Objetos que transforman secuencias de números generadas por un generador en secuencias de números que siguen una distribución de variable aleatoria específica, como Normal uniforme o Binomial.
Generadores
I. Motores de números pseudoaleatorios: Utilizan un algoritmo para generar números aleatorios basados en una semilla inicial. Estos son:

1. motor_congruencial_lineal : Es el motor más simple de la biblioteca STL que genera números enteros aleatorios sin signo. Sigue:
una forma completa
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; }
Producción:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: Es un motor de números aleatorios basado en el algoritmo Mersenne Twister. Produce números aleatorios enteros sin signo de alta calidad en el intervalo [0 (2^w)-1].
donde 'w' es el tamaño de palabra: número de bits de cada palabra en la secuencia de estado.
// 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; }
Producción:
3348201622 is a random number between 0 and 4294967295
3. restar_con_carry_motor: Es un motor generador de números pseudoaleatorios que produce números enteros sin signo.
El algoritmo utilizado es un rezagado. generador de fibonacci con una secuencia de estado de r elementos enteros más un valor de acarreo.
// 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; }
Producción:
8606455 is a random number between 0 and 16777215
II. Generador de números aleatorios : Es un generador de números aleatorios que produce números aleatorios no deterministas.
// 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; }
Producción:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Motores de números pseudoaleatorios (instanciaciones) : Estas son las instancias particulares de motores y adaptadores de generadores:

1. motor_aleatorio_predeterminado : Esta es una clase de motor de números aleatorios que genera números pseudoaleatorios.
cómo inyectar una clase abstracta simulada
La función cambia el estado interno por uno que modifica el valor del estado según el algoritmo dado:
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; }
Producción:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Genera números pseudoaleatorios; es similar a generador congruencial lineal
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; }
Producción:
películas
489592737 is a random number between 1 and 2147483646
3.MT19937: Es el generador Mersenne Twister 19937. Es un generador pseudoaleatorio de números de 32 bits con un tamaño de estado 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; }
Producción:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: Es un generador base Ranlux 24. Es un generador pseudoaleatorio de resta con acarreo de números de 24 bits que generalmente se usa como motor base para el generador ranlux24.
La función cambia el estado interno llamando a su algoritmo de transición que aplica una operación de resta con acarreo en el 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; }
Producción:
7275352 is a random number between 0 and 16777215
Se aplica un formato similar a los demás ejemplos.
IV. Adaptadores de motor

1. descartar_bloque_motor: Es una plantilla de clase de adaptador de motor que adapta un Motor generador de números pseudoaleatorios escriba usando solo elementos 'r' de cada bloque de elementos 'p' de la secuencia que produce descartando el resto.
El adaptador mantiene un recuento interno de cuántos elementos se han producido en el bloque actual.
Los generadores estándar ranlux24 y ranlux48 adaptar un restar_con_carry_motor utilizando este adaptador.
// 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; }
Producción:
8132325 is a random number between 0 and 16777215
2. motor_bits_independiente: Es una plantilla de clase de adaptador de motor que adapta un Motor generador de números pseudoaleatorios tipo para producir números aleatorios con un número específico de bits (w).
El algoritmo de transición del motor invoca al miembro operador() del motor base tantas veces como sea necesario para obtener suficientes bits significativos para construir un valor aleatorio.
// 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; }
Producción:
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: Es una plantilla de clase de adaptador de motor que adapta un Motor generador de números pseudoaleatorios escriba para que los números se entreguen en una secuencia diferente.
El objeto mantiene un búfer de k números generados internamente y cuando se solicita devuelve un número seleccionado aleatoriamente dentro del búfer reemplazándolo con un valor obtenido de su motor base.
El algoritmo de transición del motor elige un valor en la tabla interna (que devuelve la función) y lo reemplaza con un nuevo valor obtenido de su motor 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; }
Producción:
javafx
9213395 is a random number between 0 and 16777215Crear cuestionario