intestazione casuale in C++ | Impostare 1 (generatori)

intestazione casuale in C++ | Impostare 1 (generatori)

Questa intestazione introduce le funzionalità di generazione di numeri casuali. Questa libreria consente di produrre numeri casuali utilizzando combinazioni di generatori e distribuzioni.

    Generatori : Oggetti che generano numeri distribuiti uniformemente.
  • 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:

motori a numeri casuali

1. motore_lineare_congruenziale : È il motore più semplice nella libreria STL che genera numeri interi casuali senza segno. Ne consegue: 

 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  
    operatore(): Genera un numero casuale. minimo: Fornisce il valore minimo restituito dal membro operator(). massimo: Fornisce il valore massimo restituito dal membro operator().
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  ;   }   

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. 

    operatore(): Genera il numero casuale. minimo: Restituisce il valore minimo restituito dal membro operator() che per mersenne_twister_engine è sempre zero. massimo: Restituisce il valore massimo restituito dal membro operator() che per mersenne_twister_engine è 2w-1 (dove w è la dimensione della parola).
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  ;   }   

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.

    operatore() : Genera il numero casuale. massimo : Restituisce il valore massimo restituito dal membro operator() che è (2^w)-1 per subtract_with_carry_engine dove 'w' è la dimensione della parola. min : Restituisce il valore minimo restituito dal membro operator() che è sempre zero per 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  ;   }   

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.

    dispositivo_casuale : È il vero generatore di numeri casuali. operatore() : Restituisce un nuovo numero casuale. min : Restituisce il valore minimo restituito dal membro operator() che per random_device è sempre zero. massimo : Restituisce il valore massimo restituito dal membro operator().
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  ;   }   

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:

Motori con numeri pseudo-casuali (istanziazioni)

1. default_random_engine : Questa è una classe di motori di numeri casuali che genera numeri pseudo-casuali.

    min : Restituisce il valore minimo dato da operator(). massimo : Restituisce il valore massimo dato da operator(). operatore() : Restituisce un nuovo numero casuale.
    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 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  ;      }      

Produzione:

201066682 is a random number between 1 and 2147483646 

2. minstd_rand: Genera numeri pseudo casuali; è simile a generatore congruenziale lineare

    operatore(): Restituisce un nuovo numero casuale. La funzione cambia lo stato interno con uno che modifica il valore dello stato secondo il seguente algoritmo:
x = (a.x + c) mod m where x= current state value a c and m=class template parameter 
    minimo: Restituisce il valore minimo fornito dal membro operator(). massimo: Restituisce il valore massimo fornito dal membro operator() che per linear_congruential_engine è (modulus-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  ;      }   

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.

    operatore(): Genera un numero casuale. La funzione cambia lo stato interno di uno utilizzando un algoritmo di transizione che produce una torsione sull'elemento selezionato. massimo: Restituisce il valore massimo fornito da operator(). minimo: Restituisce il valore minimo fornito da 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  ;      }   

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.

    operatore(): Restituisce un nuovo numero casuale.
    La funzione modifica lo stato interno chiamando il suo algoritmo di transizione che applica un'operazione di sottrazione con riporto sull'elemento. massimo: Restituisce il valore massimo fornito da operator(). minimo: Restituisce il valore minimo fornito da 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   <  unsigned    24    10    24  >     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

intestazione casuale in C++ | Impostare 1 (generatori)

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.

I generatori standard ranlux24 E ranlux48 adattare a sottrai_con_trasporta_motore utilizzando questo adattatore.

    operatore(): Restituisce un nuovo numero casuale. massimo: Restituisce il valore massimo fornito da operator(). minimo: Restituisce il valore minimo fornito da 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  ;      }      

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).

    operatore(): Restituisce un nuovo numero casuale.
    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. massimo: Restituisce il valore massimo fornito da operator(). minimo: Restituisce il valore minimo fornito da 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   <  mt19937    64    uint_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.

    operatore(): Restituisce un nuovo numero casuale.
    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. massimo: Restituisce il valore massimo fornito da operator(). minimo: Restituisce il valore minimo fornito da 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  ;      }      

Produzione:

9213395 is a random number between 0 and 16777215 
Crea quiz