logo

capçalera aleatòria en C++ | Set 1 (generadors)

Aquesta capçalera introdueix les instal·lacions de generació de números aleatoris. Aquesta biblioteca permet produir números aleatoris mitjançant combinacions de generadors i distribucions.

    Generadors: Objectes que generen nombres uniformement distribuïts.
  • Distribucions : Objectes que transformen seqüències de nombres generades per un generador en seqüències de nombres que segueixen una distribució de variables aleatòries específica, com ara Normal uniforme o Binomi.

Generadors

I. Motors de nombres pseudoaleatoris: Utilitzen un algorisme per generar números aleatoris basats en una llavor inicial. Aquests són:



motors de nombres aleatoris' title=

1. motor_lineal_congruencial : és el motor més senzill de la biblioteca STL que genera nombres enters sense signe aleatoris. Segueix: 

llegiu el fitxer csv 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 
    operador():Genera un nombre aleatori.min:Proporciona el valor mínim retornat per l'operador membre ().màxim:Proporciona el valor màxim retornat per l'operador membre ().
C++
// 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; } 

Sortida:

211182246 is a random number between 1 and 2147483646

2. mersenne_twister_engine: És un motor de nombres aleatoris basat en l'algoritme Mersenne Twister. Produeix nombres aleatoris enters sense signe d'alta qualitat a l'interval [0 (2^w)-1].
on 'w' és la mida de la paraula: nombre de bits de cada paraula en la seqüència d'estats. 

    operador():Genera el nombre aleatori.min:Retorna el valor mínim retornat per l'operador membre () que per a mersenne_twister_engine sempre és zero.màxim:Retorna el valor màxim retornat per l'operador membre () que per a mersenne_twister_engine és 2w-1 (on w és la mida de la paraula).
C++
// 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; } 

Sortida:

3348201622 is a random number between 0 and 4294967295

3. resta_amb_carry_engine: És un motor generador de nombres pseudoaleatoris que produeix nombres enters sense signe.
L'algorisme utilitzat és un retardat generador de Fibonacci amb una seqüència d'estats de r elements enters més un valor de càrrega.

variable de referència en java
    operador (): genera el nombre aleatori.màx: retorna el valor màxim retornat per l'operador membre () que és (2^w)-1 per subtract_with_carry_engine on 'w' és la mida de la paraula.min: retorna el valor mínim retornat per l'operador membre () que sempre és zero per a subtract_with_carry_engine.
C++
// 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; } 

Sortida:

8606455 is a random number between 0 and 16777215


II. Generador de números aleatoris : És un generador de nombres aleatoris que produeix nombres aleatoris no deterministes.

    dispositiu_atzar: És el veritable generador de números aleatoris.operador (): retorna un nou nombre aleatori.min: retorna el valor mínim retornat per l'operador membre () que per a dispositiu_atzar és sempre zero.màx: retorna el valor màxim retornat per l'operador membre ().
C++
// 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; } 

Sortida:

default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883

III. Motors de nombres pseudoaleatoris (instanciacions) : Aquestes són les instàncies particulars dels motors i adaptadors del generador:

Motors de nombres pseudoaleatoris (instanciacions)' title=

1. motor_atzar_predeterminat : Aquesta és una classe de motor de nombres aleatoris que genera nombres pseudoaleatoris.

    min: retorna el valor mínim donat per operator().màx: retorna el valor màxim donat per operator().operador (): retorna un nou nombre aleatori.
    La funció canvia l'estat intern per un que modifica el valor de l'estat segons l'algorisme donat:
 x= (a.x + c)mod m Where x= current state value a and c = respective class template parameters m = class template parameter 
C++
// 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;  }  

Sortida:

201066682 is a random number between 1 and 2147483646

2. minstd_rand: Genera nombres pseudoaleatoris; és semblant a generador lineal congruencial

    operador():Retorna un nou nombre aleatori. La funció canvia l'estat intern per un que modifica el valor d'estat segons el següent algorisme:
x = (a.x + c) mod m where x= current state value a c and m=class template parameter
    min:Retorna el valor mínim donat per l'operador membre ().màxim:Retorna el valor màxim donat per l'operador membre () que per linear_congruential_engine és (mòdul-1).
C++
// 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;  } 

Sortida:

489592737 is a random number between 1 and 2147483646

3.MT19937: És el generador Mersenne Twister 19937. És un generador pseudoaleatori de números de 32 bits amb una mida d'estat de 19937 bits.

    operador():Genera un nombre aleatori. La funció canvia l'estat intern mitjançant un algorisme de transició que produeix un gir a l'element seleccionat.màxim:Retorna el valor màxim donat per operator().min:Retorna el valor mínim donat per operator().
     
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;  } 

Sortida:

tutorial d'espurna
1445431990 is a random number between 0 and 4294967295

4. ranlux24_base: És un generador base Ranlux 24. És un generador pseudoaleatori de resta amb transport de números de 24 bits que s'utilitza generalment com a motor base per al generador ranlux24.

    operador():Retorna un nou nombre aleatori.
    La funció canvia l'estat intern cridant al seu algorisme de transició que aplica una operació de resta amb transport a l'element.màxim:Retorna el valor màxim donat per operator().min:Retorna el valor mínim donat per operator().
C++
// 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;  }  

Sortida:

7275352 is a random number between 0 and 16777215

Un format similar és aplicable per a la resta d'exemples.

IV. Adaptadors de motor

capçalera aleatòria en C++ | Set 1 (generadors)

1. discard_block_engine: És una plantilla de classe d'adaptador de motor que adapta a Motor generador de nombres pseudoaleatoris escriviu utilitzant només elements 'r' de cada bloc d'elements 'p' de la seqüència que produeix descartant la resta.
L'adaptador manté un recompte intern de quants elements s'han produït al bloc actual.

codi abs c

Els generadors estàndard ranlux24 i ranlux48 adaptar a restar_amb_carry_engine utilitzant aquest adaptador.

    operador():Retorna un nou nombre aleatori.màxim:Retorna el valor màxim donat per operator().min:Retorna el valor mínim donat per operator().
C++
// 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;  }  

Sortida:

8132325 is a random number between 0 and 16777215

2. motor_de_bits_independent: És una plantilla de classe d'adaptador de motor que adapta a Motor generador de nombres pseudoaleatoris escriviu per produir números aleatoris amb un nombre específic de bits (w).

    operador():Retorna un nou nombre aleatori.
    L'algoritme de transició del motor invoca el membre operador() del motor base tantes vegades com sigui necessari per obtenir prou bits significatius per construir un valor aleatori.màxim:Retorna el valor màxim donat per operator().min:Retorna el valor mínim donat per operator().
C++
// 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;  }  

Sortida:

13551674127875514537 is a random number between 0 and 184467

3. shuffle_order_engine: És una plantilla de classe d'adaptador de motor que adapta a Motor generador de nombres pseudoaleatoris escriviu de manera que els números s'entreguin en una seqüència diferent.
L'objecte manté una memòria intermèdia de k números generats internament i quan se'l demana retorna un nombre seleccionat aleatòriament dins de la memòria intermèdia substituint-lo per un valor obtingut del seu motor base.

    operador():Retorna un nou nombre aleatori.
    L'algoritme de transició del motor tria un valor a la taula interna (que és retornat per la funció) i el substitueix per un nou valor obtingut del seu motor base.màxim:Retorna el valor màxim donat per operator().min:Retorna el valor mínim donat per operator().
C++
// 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;  }  

Sortida:

9213395 is a random number between 0 and 16777215
Crea un qüestionari