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 알고리즘을 기반으로 한 난수 엔진입니다. 간격 [0 (2^w)-1]에서 고품질의 부호 없는 정수 난수를 생성합니다.
여기서 'w'는 단어 크기입니다. 상태 시퀀스에 있는 각 단어의 비트 수입니다. 

    연산자(): 난수를 생성합니다. 최소: mersenne_twister_engine의 경우 항상 0인 연산자() 멤버가 반환한 최소값을 반환합니다. 최대: mersenne_twister_engine의 경우 2w-1(w는 단어 크기)인 Operator() 멤버가 반환한 최대값을 반환합니다.
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개의 정수 요소와 하나의 캐리 값으로 구성된 상태 시퀀스를 사용합니다.

    연산자() : 난수를 생성합니다. 최대 : 'w'가 단어 크기인 subtract_with_carry_engine에 대해 (2^w)-1인 연산자() 멤버가 반환한 최대값을 반환합니다. 분 : subtract_with_carry_engine에 대해 항상 0인 연산자() 멤버가 반환한 최소값을 반환합니다.
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. 난수 생성기 : 비결정적 난수를 생성하는 난수 생성기입니다.

    무작위_장치 : 진정한 난수 생성기입니다. 연산자() : 새로운 난수를 반환합니다. 분 : 임의의 장치에 대해 항상 0인 연산자() 멤버가 반환한 최소값을 반환합니다. 최대 : 멤버 Operator()가 반환한 최대값을 반환합니다.
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 : 의사 난수를 생성하는 난수 엔진 클래스입니다.

    분 : Operator()에서 주어진 최소값을 반환합니다. 최대 : Operator()에서 주어진 최대값을 반환합니다. 연산자() : 새로운 난수를 반환합니다.
    이 함수는 주어진 알고리즘에 따라 상태 값을 수정하는 방식으로 내부 상태를 변경합니다.
 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 
    최소: 연산자() 멤버가 제공한 최소값을 반환합니다. 최대: 이는 선형_congruential_engine에 대해 (modulus-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 생성기입니다. 상태 크기가 19937비트인 32비트 숫자의 의사 난수 생성기입니다.

    연산자(): 난수를 생성합니다. 이 기능은 선택한 요소에 비틀림을 생성하는 전환 알고리즘을 사용하여 내부 상태를 하나씩 변경합니다. 최대: Operator()에 의해 주어진 최대값을 반환합니다. 최소: 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  ;      }   

산출:

1445431990 is a random number between 0 and 4294967295 

4. ranlux24_base: Ranlux 24 베이스 생성기 입니다. 일반적으로 ranlux24 생성기의 기본 엔진으로 사용되는 24비트 숫자의 뺄셈 의사 난수 생성기입니다.

    연산자(): 새로운 난수를 반환합니다.
    이 함수는 요소에 캐리 포함 빼기 연산을 적용하는 전환 알고리즘을 호출하여 내부 상태를 변경합니다. 최대: Operator()에 의해 주어진 최대값을 반환합니다. 최소: 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  ;      }      

산출:

7275352 is a random number between 0 and 16777215 

다른 예에도 비슷한 형식이 적용됩니다.

IV. 엔진 어댑터

C++의 임의 헤더 | 세트 1(발전기)

1. 폐기_블록_엔진: 이는 다음을 적용하는 엔진 어댑터 클래스 템플릿입니다. 의사 난수 생성기 엔진 나머지는 버리고 생성된 시퀀스에서 'p' 요소의 각 블록에서 'r' 요소만 사용하여 유형을 지정합니다.
어댑터는 현재 블록에서 생성된 요소 수에 대한 내부 개수를 유지합니다.

표준 발전기 란룩스24 그리고 란룩스48 적응하다 subtract_with_carry_engine 이 어댑터를 사용하여.

    연산자(): 새로운 난수를 반환합니다. 최대: Operator()에 의해 주어진 최대값을 반환합니다. 최소: 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  ;      }      

산출:

8132325 is a random number between 0 and 16777215 

2. independent_bits_engine: 이는 다음을 적용하는 엔진 어댑터 클래스 템플릿입니다. 의사 난수 생성기 엔진 특정 수의 비트(w)를 사용하여 난수를 생성하는 유형입니다.

    연산자(): 새로운 난수를 반환합니다.
    엔진의 전환 알고리즘은 임의의 값을 구성하기에 충분한 유효 비트를 얻기 위해 필요한 만큼 기본 엔진의 Operator() 멤버를 호출합니다. 최대: Operator()에 의해 주어진 최대값을 반환합니다. 최소: 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  ;      }      

산출:

13551674127875514537 is a random number between 0 and 184467 

3. shuffle_order_engine: 이는 다음을 적용하는 엔진 어댑터 클래스 템플릿입니다. 의사 난수 생성기 엔진 숫자가 다른 순서로 전달되도록 입력하세요.
객체는 내부적으로 생성된 k개의 숫자로 구성된 버퍼를 유지하며 요청 시 버퍼 내에서 무작위로 선택된 숫자를 반환하며 이를 기본 엔진에서 얻은 값으로 대체합니다.

    연산자(): 새로운 난수를 반환합니다.
    엔진의 전환 알고리즘은 내부 테이블(함수에 의해 반환됨)에서 값을 선택하고 이를 기본 엔진에서 얻은 새 값으로 바꿉니다. 최대: Operator()에 의해 주어진 최대값을 반환합니다. 최소: 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  ;      }      

산출:

9213395 is a random number between 0 and 16777215 
퀴즈 만들기