atsitiktinė antraštė C++ | 1 rinkinys (generatoriai)
Šioje antraštėje pristatomos atsitiktinių skaičių generavimo priemonės. Ši biblioteka leidžia generuoti atsitiktinius skaičius naudojant generatorių ir skirstinių derinius.
- Paskirstymai : objektai, transformuojantys generatoriaus sugeneruotas skaičių sekas į skaičių sekas, atitinkančias konkretų atsitiktinių kintamųjų pasiskirstymą, pvz., vienodą normalųjį arba dvejetainį.
Generatoriai
I. Pseudoatsitiktinių skaičių varikliai: Jie naudoja algoritmą atsitiktiniams skaičiams generuoti pagal pradinę sėklą. Tai yra:
1. linijinis_kongruentinis_variklis : Tai paprasčiausias STL bibliotekos variklis, generuojantis atsitiktinius beženklius sveikuosius skaičius. Iš to seka:
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 ; }
Išvestis:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: Tai atsitiktinių skaičių variklis, pagrįstas Mersenne Twister algoritmu. Jis sukuria aukštos kokybės nežymius sveikuosius atsitiktinius skaičius intervale [0 (2^w)-1].
kur „w“ yra žodžio dydis: kiekvieno žodžio bitų skaičius būsenos sekoje.
// 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 ; }
Išvestis:
3348201622 is a random number between 0 and 4294967295
3. subtract_with_carry_engine: Tai pseudo-atsitiktinių skaičių generatorius, generuojantis beženklius sveikuosius skaičius.
Naudojamas atsilikęs algoritmas Fibonacci generatorius su r sveikojo skaičiaus elementų būsenų seka ir viena pernešimo reikšme.
// 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 ; }
Išvestis:
8606455 is a random number between 0 and 16777215
II. Atsitiktinių skaičių generatorius : Tai atsitiktinių skaičių generatorius, sukuriantis nedeterministinius atsitiktinius skaičius.
// 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 ; }
Išvestis:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Pseudoatsitiktinių skaičių varikliai (eksperimentai) : Tai yra konkretūs generatorių variklių ir adapterių egzemplioriai:
1. numatytasis_atsitiktinis_variklis : Tai atsitiktinių skaičių variklio klasė, generuojanti pseudoatsitiktinius skaičius.
Funkcija pakeičia vidinę būseną viena, kuri pakeičia būsenos reikšmę pagal pateiktą algoritmą:
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 ; }
Išvestis:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Jis generuoja pseudo atsitiktinius skaičius; jis panašus į tiesinis kongruentinis generatorius
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 ; }
Išvestis:
489592737 is a random number between 1 and 2147483646
3.MT19937: Tai Mersenne Twister 19937 generatorius. Tai pseudoatsitiktinis 32 bitų skaičių generatorius, kurio būsenos dydis yra 19937 bitai.
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 ; }
Išvestis:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: Tai Ranlux 24 bazinis generatorius. Tai pseudoatsitiktinis 24 bitų skaičių generatorius, paprastai naudojamas kaip pagrindinis „ranlux24“ generatoriaus variklis.
Funkcija pakeičia vidinę būseną iškviesdama savo perėjimo algoritmą, kuris elementui taiko atimties ir pernešimo operaciją.
// 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 ; }
Išvestis:
7275352 is a random number between 0 and 16777215
Panašus formatas taikomas ir kitiems pavyzdžiams.
IV. Variklio adapteriai
1. discard_block_engine: Tai variklio adapterio klasės šablonas, kuris pritaiko a pseudo-atsitiktinių skaičių generatorius Variklis įrašykite naudodami tik kiekvieno „p“ elementų bloko „r“ elementus iš sekos, kurią jis sukuria, o likusius išmeskite.
Adapteris saugo vidinį skaičių, kiek elementų buvo pagaminta dabartiniame bloke.
Standartiniai generatoriai ranlux24 ir ranlux48 pritaikyti a atimti_su_nešimo_varikliu naudojant šį adapterį.
// 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 ; }
Išvestis:
8132325 is a random number between 0 and 16777215
2. independent_bits_engine: Tai variklio adapterio klasės šablonas, kuris pritaiko a pseudo-atsitiktinių skaičių generatorius Variklis tipo, kad gautumėte atsitiktinius skaičius su tam tikru bitų skaičiumi (w).
Variklio perėjimo algoritmas iškviečia bazinio variklio operatorių() narį tiek kartų, kiek reikia, kad gautų pakankamai reikšmingų bitų atsitiktinei vertei sukurti.
// 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 ; }
Išvestis:
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: Tai variklio adapterio klasės šablonas, kuris pritaiko a pseudo-atsitiktinių skaičių generatorius Variklis įveskite, kad numeriai būtų pateikiami kita seka.
Objektas viduje saugo k sugeneruotų skaičių buferį ir paprašius grąžina atsitiktinai pasirinktą skaičių buferyje, pakeičiant jį reikšme, gauta iš pagrindinio variklio.
Variklio perėjimo algoritmas parenka reikšmę vidinėje lentelėje (kurią grąžina funkcija) ir pakeičia ją nauja verte, gauta iš pagrindinio variklio.
// 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 ; }
Išvestis:
9213395 is a random number between 0 and 16777215Sukurti viktoriną