losowy nagłówek w C++ | Zestaw 1 (Generatory)

losowy nagłówek w C++ | Zestaw 1 (Generatory)

W tym nagłówku przedstawiono możliwości generowania liczb losowych. Biblioteka ta umożliwia tworzenie liczb losowych przy użyciu kombinacji generatorów i rozkładów.

    Generatory : Obiekty generujące liczby o rozkładzie równomiernie rozłożonym.
  • Dystrybucje : Obiekty przekształcające ciągi liczb wygenerowane przez generator na ciągi liczb zgodne z określonym rozkładem zmiennej losowej, takim jak jednolity normalny lub dwumianowy.

Generatory

I. Silniki liczb pseudolosowych: Używają algorytmu do generowania liczb losowych na podstawie początkowego materiału siewnego. Są to:

silniki liczb losowych

1. silnik_spójny_liniowy : Jest to najprostszy silnik w bibliotece STL, który generuje losowe liczby całkowite bez znaku. Oto co następuje: 

 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  
    operator(): Generuje liczbę losową. min: Podaje minimalną wartość zwracaną przez operatora członkowskiego (). maks.: Podaje maksymalną wartość zwracaną przez operatora członkowskiego ().
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  ;   }   

Wyjście:

211182246 is a random number between 1 and 2147483646 

2. mersenne_twister_engine: Jest to silnik liczb losowych oparty na algorytmie Mersenne Twister. Tworzy wysokiej jakości liczby losowe w postaci liczb całkowitych bez znaku w przedziale [0 (2^w)-1].
gdzie „w” to rozmiar słowa: liczba bitów każdego słowa w sekwencji stanów. 

    operator(): Generuje liczbę losową. min: Zwraca minimalną wartość zwróconą przez operator(), która dla mersenne_twister_engine wynosi zawsze zero. maks.: Zwraca maksymalną wartość zwróconą przez operator(), która dla mersenne_twister_engine wynosi 2w-1 (gdzie w jest rozmiarem słowa).
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  ;   }   

Wyjście:

3348201622 is a random number between 0 and 4294967295 

3. odejmij_z_nośnym_silnikiem: Jest to silnik generatora liczb pseudolosowych, który generuje liczby całkowite bez znaku.
Zastosowany algorytm jest opóźniony generator Fibonacciego z sekwencją stanu składającą się z r elementów całkowitych plus jedna wartość przeniesienia.

    operator() : Generuje liczbę losową. maks : Zwraca maksymalną wartość zwróconą przez element operator(), która wynosi (2^w)-1 dla subtract_with_carry_engine, gdzie „w” to rozmiar słowa. min : Zwraca minimalną wartość zwróconą przez operator(), która dla subtract_with_carry_engine zawsze wynosi zero.
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  ;   }   

Wyjście:

8606455 is a random number between 0 and 16777215 


II. Generator liczb losowych : Jest to generator liczb losowych, który generuje niedeterministyczne liczby losowe.

    losowe_urządzenie : To jest prawdziwy generator liczb losowych. operator() : Zwraca nową liczbę losową. min : Zwraca minimalną wartość zwróconą przez operatora(), która dla random_device wynosi zawsze zero. maks : Zwraca maksymalną wartość zwróconą przez operatora elementu członkowskiego ().
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  ;   }   

Wyjście:

default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883 

III. Silniki liczb pseudolosowych (instancje) : Oto konkretne instancje silników generatorów i adapterów:

Silniki liczb pseudolosowych (instancje)

1. domyślny_losowy_silnik : Jest to klasa silnika liczb losowych, która generuje liczby pseudolosowe.

    min : Zwraca minimalną wartość podaną przez operator(). maks : Zwraca maksymalną wartość podaną przez operator(). operator() : Zwraca nową liczbę losową.
    Funkcja zmienia stan wewnętrzny o jeden, co modyfikuje wartość stanu zgodnie z zadanym algorytmem:
 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  ;      }      

Wyjście:

201066682 is a random number between 1 and 2147483646 

2. minstd_rand: Generuje liczby pseudolosowe; jest podobne do liniowy generator kongruencjalny

    operator(): Zwraca nową liczbę losową. Funkcja zmienia stan wewnętrzny o jeden, co modyfikuje wartość stanu według następującego algorytmu:
x = (a.x + c) mod m where x= current state value a c and m=class template parameter 
    min: Zwraca minimalną wartość podaną przez operatora(). maks.: Zwraca maksymalną wartość podaną przez operator(), która dla silnika linear_congruential_engine wynosi (moduł-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  ;      }   

Wyjście:

489592737 is a random number between 1 and 2147483646 

3.MT19937: Jest to generator Mersenne Twister 19937. Jest to generator pseudolosowy liczb 32-bitowych o rozmiarze stanu wynoszącym 19937 bitów.

    operator(): Generuje liczbę losową. Funkcja zmienia stan wewnętrzny o jeden za pomocą algorytmu przejścia, który powoduje skręcenie wybranego elementu. maks.: Zwraca maksymalną wartość podaną przez operator(). min: Zwraca minimalną wartość podaną przez operator().
     
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  ;      }   

Wyjście:

1445431990 is a random number between 0 and 4294967295 

4. ranlux24_base: Jest to generator bazowy Ranlux 24. Jest to pseudolosowy generator liczb 24-bitowych z funkcją odejmowania i przenoszenia, powszechnie używany jako podstawowy silnik generatora ranlux24.

    operator(): Zwraca nową liczbę losową.
    Funkcja zmienia stan wewnętrzny wywołując algorytm przejścia, który stosuje na elemencie operację odejmowania z przeniesieniem. maks.: Zwraca maksymalną wartość podaną przez operator(). min: Zwraca minimalną wartość podaną przez operator().
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  ;      }      

Wyjście:

7275352 is a random number between 0 and 16777215 

Podobny format ma zastosowanie w przypadku pozostałych przykładów.

IV. Adaptery silnika

losowy nagłówek w C++ | Zestaw 1 (Generatory)

1. cancel_block_engine: Jest to szablon klasy adaptera silnika, który dostosowuje plik Generator liczb pseudolosowych Silnik wpisz, używając tylko elementów „r” z każdego bloku elementów „p” z utworzonej sekwencji, odrzucając resztę.
Adapter prowadzi wewnętrzne zliczanie liczby elementów wyprodukowanych w bieżącym bloku.

Standardowe generatory ranlux24 I ranlux48 dostosować się subtract_with_carry_engine za pomocą tego adaptera.

    operator(): Zwraca nową liczbę losową. maks.: Zwraca maksymalną wartość podaną przez operator(). min: Zwraca minimalną wartość podaną przez operator().
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  ;      }      

Wyjście:

8132325 is a random number between 0 and 16777215 

2. niezależny_bits_engine: Jest to szablon klasy adaptera silnika, który dostosowuje plik Generator liczb pseudolosowych Silnik type, aby wygenerować liczby losowe z określoną liczbą bitów (w).

    operator(): Zwraca nową liczbę losową.
    Algorytm przejścia silnika wywołuje element operator() silników podstawowych tyle razy, ile jest to konieczne, aby uzyskać wystarczającą liczbę znaczących bitów do skonstruowania wartości losowej. maks.: Zwraca maksymalną wartość podaną przez operator(). min: Zwraca minimalną wartość podaną przez operator().
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  ;      }      

Wyjście:

13551674127875514537 is a random number between 0 and 184467 

3. shuffle_order_engine: Jest to szablon klasy adaptera silnika, który dostosowuje plik Generator liczb pseudolosowych Silnik wpisz tak, aby liczby były dostarczane w innej kolejności.
Obiekt przechowuje wewnętrznie bufor k wygenerowanych liczb i na żądanie zwraca losowo wybraną liczbę w obrębie bufora, zastępując ją wartością uzyskaną z jego podstawowego silnika.

    operator(): Zwraca nową liczbę losową.
    Algorytm przejścia silnika wybiera wartość z wewnętrznej tabeli (która jest zwracana przez funkcję) i zastępuje ją nową wartością uzyskaną z silnika bazowego. maks.: Zwraca maksymalną wartość podaną przez operator(). min: Zwraca minimalną wartość podaną przez operator().
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  ;      }      

Wyjście:

9213395 is a random number between 0 and 16777215 
Utwórz quiz