izlases galvene C++ | 1. komplekts (ģeneratori)

izlases galvene C++ | 1. komplekts (ģeneratori)

Šī galvene ievieš nejaušu skaitļu ģenerēšanas iespējas. Šī bibliotēka ļauj iegūt nejaušus skaitļus, izmantojot ģeneratoru un sadalījumu kombinācijas.

    Ģeneratori : objekti, kas ģenerē vienmērīgi sadalītus skaitļus.
  • Izplatījumi : objekti, kas pārveido ģeneratora ģenerētās skaitļu virknes skaitļu secībās, kas atbilst noteiktam nejaušā mainīgā sadalījumam, piemēram, vienmērīgam normālam vai binomiālam.

Ģeneratori

I. Pseidogadījuma skaitļu dzinēji: Viņi izmanto algoritmu, lai ģenerētu nejaušus skaitļus, pamatojoties uz sākotnējo sēklu. Tie ir:

nejaušu skaitļu dzinēji

1. lineārais_kongruenciālais_dzinējs : tas ir vienkāršākais dzinējs STL bibliotēkā, kas ģenerē nejaušus neparakstītus veselus skaitļus. No tā izriet: 

 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  
    operators(): Tas ģenerē nejaušu skaitu. min: Tas dod minimālo vērtību, ko atgriež dalībnieka operators (). maks.: Tas dod maksimālo vērtību, ko atgriež dalībnieka operators ().
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  ;   }   

Izvade:

211182246 is a random number between 1 and 2147483646 

2. mersenne_twister_engine: Tas ir nejaušu skaitļu dzinējs, kura pamatā ir Mersenne Twister algoritms. Tas rada augstas kvalitātes neparakstītus veselus nejaušus skaitļus intervālā [0 (2^w)-1].
kur “w” ir vārda lielums: katra vārda bitu skaits stāvokļa secībā. 

    operators(): Tas ģenerē nejaušo skaitli. min: Tas atgriež minimālo vērtību, ko atgriež dalībnieks operators (), kas mersenne_twister_engine vienmēr ir nulle. maks.: Tas atgriež maksimālo vērtību, ko atgriež dalībnieka operators (), kas mersenne_twister_engine ir 2w-1 (kur w ir vārda lielums).
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  ;   }   

Izvade:

3348201622 is a random number between 0 and 4294967295 

3. subtract_with_carry_engine: Tas ir pseidogadījuma skaitļu ģenerators, kas rada neparakstītus veselus skaitļus.
Izmantotais algoritms ir aizkavējies Fibonači ģenerators ar stāvokļu secību r veselu skaitļu elementu plus viena pārneses vērtība.

    operators() : tas ģenerē nejaušu skaitli. maks : Tas atgriež maksimālo vērtību, ko atgriež dalībnieks operators(), kas ir (2^w)-1 subtract_with_carry_engine, kur “w” ir vārda lielums. min : Tas atgriež minimālo vērtību, ko atgriež dalībnieks operators(), kas vienmēr ir nulle atņemt_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  ;   }   

Izvade:

8606455 is a random number between 0 and 16777215 


II. Nejaušo skaitļu ģenerators : tas ir nejaušu skaitļu ģenerators, kas rada nedeterminētus nejaušus skaitļus.

    random_device : Tas ir patiesais nejaušo skaitļu ģenerators. operators() : tas atgriež jaunu nejaušu skaitli. min : Tas atgriež minimālo vērtību, ko atgriež dalībnieks operators(), kas izlases_ierīcei vienmēr ir nulle. maks : Tas atgriež maksimālo vērtību, ko atgriež dalībnieka operators ().
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  ;   }   

Izvade:

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

III. Pseidogadījuma skaitļu dzinēji (instantiācijas) : Šīs ir ģeneratoru dzinēju un adapteru īpašās instancijas:

Pseidogadījuma skaitļu dzinēji (instantiācijas)

1. noklusējuma_gadījuma_dzinējs : šī ir nejaušu skaitļu dzinēju klase, kas ģenerē pseidogadījuma skaitļus.

    min : atgriež operatora () norādīto minimālo vērtību. maks : Tas atgriež operatora () norādīto maksimālo vērtību. operators() : tas atgriež jaunu nejaušu skaitli.
    Funkcija maina iekšējo stāvokli par vienu, kas maina stāvokļa vērtību saskaņā ar doto algoritmu:
 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  ;      }      

Izvade:

201066682 is a random number between 1 and 2147483646 

2. minstd_rand: Tas ģenerē pseido nejaušus skaitļus; tas ir līdzīgs lineārais kongruenciālais ģenerators

    operators(): Tas atgriež jaunu nejaušu skaitli. Funkcija maina iekšējo stāvokli par vienu, kas maina stāvokļa vērtību saskaņā ar šādu algoritmu:
x = (a.x + c) mod m where x= current state value a c and m=class template parameter 
    min: Tas atgriež minimālo vērtību, ko norādījis dalībnieka operators (). maks.: Tas atgriež maksimālo vērtību, ko sniedz dalībnieks operators (), kas lineārai_kongruential_engine ir (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  ;      }   

Izvade:

489592737 is a random number between 1 and 2147483646 

3.MT19937: Tas ir Mersenne Twister 19937 ģenerators. Tas ir 32 bitu skaitļu pseidogadījuma ģenerators ar stāvokļa lielumu 19937 biti.

    operators(): Tas ģenerē nejaušu skaitli. Funkcija maina iekšējo stāvokli par vienu, izmantojot pārejas algoritmu, kas rada vērpjot atlasītajā elementā. maks.: Tas atgriež operatora () norādīto maksimālo vērtību. min: Tas atgriež operatora () norādīto minimālo vērtību.
     
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  ;      }   

Izvade:

1445431990 is a random number between 0 and 4294967295 

4. ranlux24_base: Tas ir Ranlux 24 bāzes ģenerators. Tas ir 24 bitu skaitļu pseidogadījuma ģenerators ar atņemšanu ar pārnesi, ko parasti izmanto kā ranlux24 ģeneratora bāzes dzinēju.

    operators(): Tas atgriež jaunu nejaušu skaitli.
    Funkcija maina iekšējo stāvokli, izsaucot savu pārejas algoritmu, kas elementam piemēro atņemšanas ar pārnešanas darbību. maks.: Tas atgriež operatora () norādīto maksimālo vērtību. min: Tas atgriež operatora () norādīto minimālo vērtību.
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  ;      }      

Izvade:

7275352 is a random number between 0 and 16777215 

Līdzīgs formāts ir piemērojams arī citiem piemēriem.

IV. Dzinēja adapteri

izlases galvene C++ | 1. komplekts (ģeneratori)

1. discard_block_engine: Tā ir dzinēja adaptera klases veidne, kas pielāgo a pseidogadījuma skaitļu ģenerators Dzinējs ierakstiet, izmantojot tikai “r” elementus katrā “p” elementu blokā no secības, ko tas rada, pārējos izmetot.
Adapteris saglabā iekšējo uzskaiti, cik daudz elementu ir saražoti pašreizējā blokā.

Standarta ģeneratori ranlux24 un ranlux48 pielāgot a atņemt_ar_carry_engine izmantojot šo adapteri.

    operators(): Tas atgriež jaunu nejaušu skaitli. maks.: Tas atgriež operatora () norādīto maksimālo vērtību. min: Tas atgriež operatora () norādīto minimālo vērtību.
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  ;      }      

Izvade:

8132325 is a random number between 0 and 16777215 

2. independent_bits_engine: Tā ir dzinēja adaptera klases veidne, kas pielāgo a pseidogadījuma skaitļu ģenerators Dzinējs tipa, lai iegūtu nejaušus skaitļus ar noteiktu bitu skaitu (w).

    operators(): Tas atgriež jaunu nejaušu skaitli.
    Dzinēja pārejas algoritms izsauc bāzes dzinēju operatora() locekli tik reižu, cik nepieciešams, lai iegūtu pietiekami daudz nozīmīgu bitu, lai izveidotu nejaušu vērtību. maks.: Tas atgriež operatora () norādīto maksimālo vērtību. min: Tas atgriež operatora () norādīto minimālo vērtību.
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  ;      }      

Izvade:

13551674127875514537 is a random number between 0 and 184467 

3. shuffle_order_engine: Tā ir dzinēja adaptera klases veidne, kas pielāgo a pseidogadījuma skaitļu ģenerators Dzinējs ierakstiet, lai numuri tiktu piegādāti citā secībā.
Objekts iekšēji saglabā k ģenerētu skaitļu buferi un pēc pieprasījuma atgriež buferī nejauši izvēlētu skaitli, aizstājot to ar vērtību, kas iegūta no tā bāzes dzinēja.

    operators(): Tas atgriež jaunu nejaušu skaitli.
    Dzinēja pārejas algoritms izvēlas vērtību iekšējā tabulā (kuru atgriež funkcija) un aizstāj to ar jaunu vērtību, kas iegūta no tā bāzes dzinēja. maks.: Tas atgriež operatora () norādīto maksimālo vērtību. min: Tas atgriež operatora () norādīto minimālo vērtību.
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  ;      }      

Izvade:

9213395 is a random number between 0 and 16777215 
Izveidojiet viktorīnu