Denna rubrik introducerar slumptalsgenereringsmöjligheter. Detta bibliotek gör det möjligt att producera slumptal genom att använda kombinationer av generatorer och distributioner.
- Distributioner : Objekt som omvandlar talsekvenser som genereras av en generator till talsekvenser som följer en specifik slumpvariabelfördelning som likformig normal eller binomial.
Generatorer
I. Pseudo-slumptalsmotorer: De använder en algoritm för att generera slumpmässiga tal baserat på ett initialt frö. Dessa är:

1. linjär_kongruentiell_motor : Det är den enklaste motorn i STL-biblioteket som genererar slumpmässiga heltal utan tecken. Det följer:
exempel på java-kod
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; }
Produktion:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: Det är en slumptalsmotor baserad på Mersenne Twister-algoritmen. Den producerar osignerade heltalsslumptal av hög kvalitet i intervallet [0 (2^w)-1].
där 'w' är ordstorlek: Antal bitar av varje ord i tillståndssekvensen.
// 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; }
Produktion:
3348201622 is a random number between 0 and 4294967295
3. subtrahera_med_bärmotor: Det är en pseudo-slumptalsgenerator som producerar heltal utan tecken.
Algoritmen som används är en lagd fibonacci generator med en tillståndssekvens av r heltalselement plus ett bärvärde.
// 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; }
Produktion:
8606455 is a random number between 0 and 16777215
II. Generator av slumptal : Det är en slumptalsgenerator som producerar icke-deterministiska slumptal.
// 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; }
Produktion:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. Pseudo-slumptalsmotorer (instansieringar) : Det här är de speciella instansieringarna av generatormotorer och adaptrar:
fjäderverktygssvit

1. default_random_engine : Detta är en motorklass för slumptal som genererar pseudoslumptal.
Funktionen ändrar det interna tillståndet med en som modifierar tillståndsvärdet enligt den givna algoritmen:
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; }
Produktion:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: Den genererar pseudoslumptal; det liknar linjär kongruentialgenerator
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; }
Produktion:
489592737 is a random number between 1 and 2147483646
3.MT19937: Det är Mersenne Twister 19937 generator. Det är en pseudo-slumpgenerator av 32-bitars nummer med en tillståndsstorlek på 19937 bitar.
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; }
Produktion:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: Det är Ranlux 24 basgenerator. Det är en subtrahera-med-bära pseudoslumpgenerator av 24-bitars tal som vanligtvis används som basmotor för ranlux24-generatorn.
Funktionen ändrar det interna tillståndet genom att anropa dess övergångsalgoritm som tillämpar en subtrahera-med-bär-operation på elementet.
// 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; }
Produktion:
Hur man laddar ner youtube video vlc
7275352 is a random number between 0 and 16777215
Liknande format är tillämpligt för de andra exemplen.
IV. Motoradaptrar

1. discard_block_engine: Det är en mall för motoradapterklass som anpassar en pseudo-slumptalsgenerator Motor typ genom att endast använda 'r'-element i varje block av 'p'-element från sekvensen den producerar och kassera resten.
Adaptern håller en intern räkning av hur många element som har producerats i det aktuella blocket.
Standardgeneratorerna ranlux24 och ranlux48 anpassa a subtrahera_med_bärmotor med denna 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; }
Produktion:
8132325 is a random number between 0 and 16777215
2. independent_bits_engine: Det är en mall för motoradapterklass som anpassar en pseudo-slumptalsgenerator Motor typ för att producera slumptal med ett specifikt antal bitar (w).
Motorns övergångsalgoritm anropar basmotorernas operator()-medlem så många gånger som behövs för att erhålla tillräckligt med signifikanta bitar för att konstruera ett slumpmässigt värde.
// 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; }
Produktion:
inkapslingsprogram
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: Det är en mall för motoradapterklass som anpassar en pseudo-slumptalsgenerator Motor typ så att numren levereras i en annan ordningsföljd.
Objektet behåller en buffert med k genererade nummer internt och när det efterfrågas returnerar det ett slumpmässigt valt nummer i bufferten som ersätter det med ett värde som erhålls från dess basmotor.
Motorns övergångsalgoritm väljer ett värde i den interna tabellen (som returneras av funktionen) och ersätter det med ett nytt värde som erhålls från dess basmotor.
// 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; }
Produktion:
9213395 is a random number between 0 and 16777215Skapa frågesport