כותרת אקראית ב-C++ | סט 1 (גנרטורים)

כותרת אקראית ב-C++ | סט 1 (גנרטורים)

כותרת זו מציגה מתקנים ליצירת מספרים אקראיים. ספריה זו מאפשרת לייצר מספרים אקראיים באמצעות שילובים של גנרטורים והפצות.

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

גנרטורים

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

מנועי מספר אקראי

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

 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++
   // 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' הוא גודל מילה: מספר סיביות של כל מילה ברצף המצב. 

    מַפעִיל(): זה מייצר את המספר האקראי. דקה: הוא מחזיר את הערך המינימלי המוחזר על ידי ה-member operator() אשר עבור mersenne_twister_engine הוא תמיד אפס. מקסימום: הוא מחזיר את הערך המקסימלי המוחזר על ידי מפעיל חבר() אשר עבור mersenne_twister_engine הוא 2w-1 (כאשר w הוא גודל המילה).
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  ;   }   

תְפוּקָה:

3348201622 is a random number between 0 and 4294967295 

3. subtract_with_carry_engine: זהו מנוע מחולל מספרים פסאודו-אקראיים המייצר מספרים שלמים ללא סימן.
האלגוריתם בו נעשה שימוש הוא בפיגור מחולל פיבונאצי עם רצף מצב של r אלמנטים שלמים בתוספת ערך נשיאה אחד.

    מַפעִיל() : זה יוצר את המספר האקראי. מקסימום : הוא מחזיר את הערך המקסימלי המוחזר על ידי אופרטור חבר() שהוא (2^w)-1 עבור subtract_with_carry_engine כאשר 'w' הוא גודל המילה. דקה : הוא מחזיר את הערך המינימלי המוחזר על ידי חבר () שהוא תמיד אפס עבור subtract_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  ;   }   

תְפוּקָה:

8606455 is a random number between 0 and 16777215 


II. מחולל מספרים אקראיים : זהו מחולל מספרים אקראיים שמייצר מספרים אקראיים לא דטרמיניסטיים.

    מכשיר_אקראי : זהו מחולל המספרים האקראיים האמיתיים. מַפעִיל() : הוא מחזיר מספר אקראי חדש. דקה : הוא מחזיר את הערך המינימלי המוחזר על ידי ה-member operator() אשר עבור random_device הוא תמיד אפס. מקסימום : הוא מחזיר את הערך המקסימלי שהוחזר על ידי אופרטור חבר().
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  ;   }   

תְפוּקָה:

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 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  ;      }      

תְפוּקָה:

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 
    דקה: הוא מחזיר את הערך המינימלי שניתן על ידי מפעיל חבר(). מקסימום: הוא מחזיר את הערך המקסימלי שניתן על ידי אופרטור חבר() אשר עבור מנוע_קונגרונטיאלי_קומוני הוא (מודול-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  ;      }   

תְפוּקָה:

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++
   // 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  ;      }      

תְפוּקָה:

7275352 is a random number between 0 and 16777215 

פורמט דומה ישים עבור הדוגמאות האחרות.

IV. מתאמי מנוע

כותרת אקראית ב-C++ | סט 1 (גנרטורים)

1. discard_block_engine: זוהי תבנית כיתת מתאם מנוע שמתאימה את א מנוע מחולל מספרים פסאודו-אקראיים הקלד על ידי שימוש רק ברכיבי 'r' של כל בלוק של רכיבי 'p' מהרצף שהוא מייצר תוך השלכת השאר.
המתאם שומר ספירה פנימית של כמה אלמנטים יוצרו בבלוק הנוכחי.

הגנרטורים הסטנדרטיים ranlux24 ו ranlux48 להתאים א subtract_with_carry_motor באמצעות מתאם זה.

    מַפעִיל(): זה מחזיר מספר אקראי חדש. מקסימום: הוא מחזיר את הערך המקסימלי שניתן על ידי האופרטור(). דקה: הוא מחזיר את הערך המינימלי שניתן על ידי האופרטור().
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  ;      }      

תְפוּקָה:

8132325 is a random number between 0 and 16777215 

2. independent_bits_engine: זוהי תבנית כיתת מתאם מנוע שמתאימה את א מנוע מחולל מספרים פסאודו-אקראיים סוג כדי לייצר מספרים אקראיים עם מספר מסוים של סיביות (w).

    מַפעִיל(): זה מחזיר מספר אקראי חדש.
    אלגוריתם המעבר של המנוע מפעיל את חבר האופרטור() של מנועי הבסיס כמה פעמים שצריך כדי להשיג מספיק ביטים משמעותיים כדי לבנות ערך אקראי. מקסימום: הוא מחזיר את הערך המקסימלי שניתן על ידי האופרטור(). דקה: הוא מחזיר את הערך המינימלי שניתן על ידי האופרטור().
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  ;      }      

תְפוּקָה:

13551674127875514537 is a random number between 0 and 184467 

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

    מַפעִיל(): זה מחזיר מספר אקראי חדש.
    אלגוריתם המעבר של המנוע בוחר ערך בטבלה הפנימית (אשר מוחזר על ידי הפונקציה) ומחליף אותו בערך חדש המתקבל מהמנוע הבסיסי שלו. מקסימום: הוא מחזיר את הערך המקסימלי שניתן על ידי האופרטור(). דקה: הוא מחזיר את הערך המינימלי שניתן על ידי האופרטור().
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  ;      }      

תְפוּקָה:

9213395 is a random number between 0 and 16777215 
צור חידון