כותרת זו מציגה מתקנים ליצירת מספרים אקראיים. ספריה זו מאפשרת לייצר מספרים אקראיים באמצעות שילובים של גנרטורים והפצות.
- הפצות : אובייקטים הממירים רצפים של מספרים שנוצרו על ידי מחולל לרצפים של מספרים העוקבים אחר התפלגות משתנה אקראית ספציפית כגון רגיל או בינומי אחיד.
גנרטורים
I. מנועי מספרים פסאודו-אקראיים: הם משתמשים באלגוריתם כדי ליצור מספרים אקראיים המבוססים על זרע ראשוני. אלה הם:

1. מנוע_קוגרונטי ליניארי : זהו המנוע הפשוט ביותר בספריית STL שיוצר מספרים שלמים אקראיים ללא סימנים. כדלקמן:
מבנה java
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; }
תְפוּקָה:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: זהו מנוע מספר אקראי המבוסס על אלגוריתם Mersenne Twister. הוא מייצר מספרים אקראיים שלמים ללא סימן באיכות גבוהה במרווח [0 (2^w)-1].
כאשר 'w' הוא גודל מילה: מספר סיביות של כל מילה ברצף המצב.
// 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; }
תְפוּקָה:
3348201622 is a random number between 0 and 4294967295
3. subtract_with_carry_engine: זהו מנוע מחולל מספרים פסאודו-אקראיים המייצר מספרים שלמים ללא סימן.
האלגוריתם בו נעשה שימוש הוא בפיגור מחולל פיבונאצי עם רצף מצב של r אלמנטים שלמים בתוספת ערך נשיאה אחד.
// 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; }
תְפוּקָה:
8606455 is a random number between 0 and 16777215
II. מחולל מספרים אקראיים : זהו מחולל מספרים אקראיים שמייצר מספרים אקראיים לא דטרמיניסטיים.
// 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; }
תְפוּקָה:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
III. מנועי מספרים פסאודו-אקראיים (מופעים) : אלו הם המופעים המיוחדים של מנועי גנרטורים ומתאמים:
מה גודל מסך המחשב שלי

1. default_random_engine : זוהי מחלקת מנוע של מספרים אקראיים שיוצרת מספרים פסאודו אקראיים.
הפונקציה משנה את המצב הפנימי באחד שמשנה את ערך המצב לפי האלגוריתם הנתון:
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; }
תְפוּקָה:
201066682 is a random number between 1 and 2147483646
2. minstd_rand: זה יוצר מספרים פסאודו אקראיים; זה דומה ל מחולל קונבנציונלי ליניארי
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; }
תְפוּקָה:
פקודת sed
489592737 is a random number between 1 and 2147483646
3.MT19937: זהו מחולל Mersenne Twister 19937. זהו מחולל פסאודו אקראי של מספרים של 32 סיביות עם גודל מצב של 19937 סיביות.
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; }
תְפוּקָה:
1445431990 is a random number between 0 and 4294967295
4. ranlux24_base: זהו גנרטור בסיס Ranlux 24. זהו מחולל פסאודו אקראי של חיסור עם נשיאה של מספרים של 24 סיביות המשמש בדרך כלל כמנוע הבסיס של מחולל ranlux24.
הפונקציה משנה את המצב הפנימי על ידי קריאת אלגוריתם המעבר שלה שמחיל פעולת חיסור עם נשיאה על האלמנט.
// 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; }
תְפוּקָה:
7275352 is a random number between 0 and 16777215
פורמט דומה ישים עבור הדוגמאות האחרות.
IV. מתאמי מנוע

1. discard_block_engine: זוהי תבנית כיתת מתאם מנוע שמתאימה את א מנוע מחולל מספרים פסאודו-אקראיים הקלד על ידי שימוש רק ברכיבי 'r' של כל בלוק של רכיבי 'p' מהרצף שהוא מייצר תוך השלכת השאר.
המתאם שומר ספירה פנימית של כמה אלמנטים יוצרו בבלוק הנוכחי.
הגנרטורים הסטנדרטיים ranlux24 ו ranlux48 להתאים א subtract_with_carry_motor באמצעות מתאם זה.
// 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; }
תְפוּקָה:
8132325 is a random number between 0 and 16777215
2. independent_bits_engine: זוהי תבנית כיתת מתאם מנוע שמתאימה את א מנוע מחולל מספרים פסאודו-אקראיים סוג כדי לייצר מספרים אקראיים עם מספר מסוים של סיביות (w).
אלגוריתם המעבר של המנוע מפעיל את חבר האופרטור() של מנועי הבסיס כמה פעמים שצריך כדי להשיג מספיק ביטים משמעותיים כדי לבנות ערך אקראי.
// 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; }
תְפוּקָה:
המרת מחרוזת ל-json ב-java
13551674127875514537 is a random number between 0 and 184467
3. shuffle_order_engine: זוהי תבנית כיתת מתאם מנוע שמתאימה את א מנוע מחולל מספרים פסאודו-אקראיים הקלד כך שהמספרים יימסרו ברצף אחר.
האובייקט שומר מאגר של k מספרים שנוצרו באופן פנימי וכאשר מתבקש מחזיר מספר שנבחר באקראי בתוך המאגר ומחליף אותו בערך המתקבל מהמנוע הבסיסי שלו.
אלגוריתם המעבר של המנוע בוחר ערך בטבלה הפנימית (אשר מוחזר על ידי הפונקציה) ומחליף אותו בערך חדש המתקבל מהמנוע הבסיסי שלו.
// 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; }
תְפוּקָה:
9213395 is a random number between 0 and 16777215צור חידון