Tämä otsikko esittelee satunnaislukujen generointitoiminnot. Tämän kirjaston avulla voidaan tuottaa satunnaislukuja generaattoreiden ja jakaumien yhdistelmillä.
- Jakelut : Objektit, jotka muuntavat generaattorin luomia lukusarjoja numerosarjoiksi, jotka noudattavat tiettyä satunnaismuuttujajakaumaa, kuten tasaista normaalia tai binomiaalista.
Generaattorit
I. Pseudosatunnaislukumoottorit: He käyttävät algoritmia satunnaislukujen luomiseen alkuperäisen siemenen perusteella. Nämä ovat:

1. lineaarinen_kongruential_moottori : Se on STL-kirjaston yksinkertaisin moottori, joka luo satunnaisia etumerkittömiä kokonaislukuja. Siitä seuraa:
isäntä linux
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; }
Lähtö:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: Se on Mersenne Twister -algoritmiin perustuva satunnaislukumoottori. Se tuottaa korkealaatuisia etumerkittömiä kokonaislukuja satunnaislukuja välillä [0 (2^w)-1].
missä 'w' on sanan koko: Tilasekvenssin kunkin sanan bittien määrä.
// 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; }
Lähtö:
3348201622 is a random number between 0 and 4294967295
3. subtract_with_carry_engine: Se on pseudosatunnaislukugeneraattori, joka tuottaa etumerkittömiä kokonaislukuja.
Käytetty algoritmi on viivästynyt fibonacci generaattori tilasekvenssillä r kokonaislukuelementtiä plus yksi siirtoarvo.
konekirjoituskytkin
// 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; }
Lähtö:
8606455 is a random number between 0 and 16777215
II. Satunnaislukugeneraattori : Se on satunnaislukugeneraattori, joka tuottaa ei-deterministisiä satunnaislukuja.
// 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; }
Lähtö:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Pseudosatunnaislukumoottorit (instanssit) : Nämä ovat generaattorimoottoreiden ja sovittimien erityiset muodot:

1. default_random_engine : Tämä on satunnaislukumoottoriluokka, joka luo pseudosatunnaislukuja.
Funktio muuttaa sisäistä tilaa yhdellä, joka muuttaa tilan arvoa annetun algoritmin mukaisesti:
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; }
Lähtö:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Se luo pseudosatunnaislukuja; se on samanlainen kuin lineaarinen kongruenssigeneraattori
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; }
Lähtö:
489592737 is a random number between 1 and 2147483646
3.MT19937: Se on Mersenne Twister 19937 -generaattori. Se on näennäissatunnainen 32-bittisten lukujen generaattori, jonka tilakoko on 19937 bittiä.
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; }
Lähtö:
rujira banerjee
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: Se on Ranlux 24 -perusgeneraattori. Se on 24-bittisten lukujen pseudosatunnaisgeneraattori, jota käytetään yleisesti ranlux24-generaattorin perusmoottorina.
Funktio muuttaa sisäistä tilaa kutsumalla siirtymäalgoritmiaan, joka soveltaa elementtiin vähennys ja siirto -operaatiota.
// 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; }
Lähtö:
7275352 is a random number between 0 and 16777215
Samanlainen muoto on sovellettavissa muihin esimerkeihin.
IV. Moottorin sovittimet

1. discard_block_engine: Se on moottorisovittimen luokan malli, joka mukauttaa a pseudosatunnaislukugeneraattori Moottori kirjoita käyttämällä vain "r"-elementtejä kustakin "p"-elementtien lohkosta sen tuottamasta sekvenssistä ja hylkää loput.
Sovitin pitää sisäisen laskelman siitä, kuinka monta elementtiä on tuotettu nykyisessä lohkossa.
merkki int javaan
Tavalliset generaattorit ranlux24 ja ranlux48 mukauttaa a vähennä_kantomoottorilla käyttämällä tätä sovitinta.
// 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; }
Lähtö:
8132325 is a random number between 0 and 16777215
2. riippumaton_bits_engine: Se on moottorisovittimen luokan malli, joka mukauttaa a pseudosatunnaislukugeneraattori Moottori tyyppi tuottaa satunnaislukuja tietyllä määrällä bittejä (w).
Moottorin siirtymäalgoritmi kutsuu peruskoneiden operaattori()-jäsenen niin monta kertaa kuin tarvitaan saadakseen tarpeeksi merkittäviä bittejä satunnaisarvon muodostamiseksi.
// 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; }
Lähtö:
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: Se on moottorisovittimen luokan malli, joka mukauttaa a pseudosatunnaislukugeneraattori Moottori kirjoita niin, että numerot toimitetaan eri järjestyksessä.
Objekti säilyttää sisäisesti k generoidun luvun puskurin ja palauttaa pyydettäessä satunnaisesti valitun luvun puskurissa ja korvaa sen perusmoottoristaan saadulla arvolla.
Moottorin siirtymäalgoritmi poimii arvon sisäisestä taulukosta (jonka funktio palauttaa) ja korvaa sen uudella arvolla, joka on saatu sen perusmoottorista.
// 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; }
Lähtö:
9213395 is a random number between 0 and 16777215Luo tietokilpailu