C++ 난수 생성 (Random Number Generation)

2021. 3. 11. 16:32·C++/Library

난수 생성 (Random Number Generation)

Linear congruential generator

  • 선형 합동 생성기

    • 널리 알려진 유사난수 생성기이다.
    unsigned int    PRNG()  // Pseudo Random Number Generator
    {
        static unsigned int seed = 5523;  // seed number
    
        seed = 8253729 * seed + 2396403;
        return (seed % 32768);
    }

std::rand

  • std::rand() 함수로 나올 수 있는 최댓값인 RAND_MAX를 이용하여 범위를 제한한다.

    • 고르게 분포되지는 않는다.
    #include <iostream>
    
    int getRandomNumber(int min, int max)
    {
        static const double fraction = 1.0 / (RAND_MAX + 1.0);
    
        return (min + static_cast<int>((max - min + 1) * \
                                        std::rand() * fraction));
    }
    
    int main()
    {
        using namespace std;
    
        for (int i = 0; i < 10; ++i)
            cout << getRandomNumber(5, 8) << '\n';
    }

<random>

  • C++11

  • 위의 경우들보다 더 정교하다.

  • Mersenne Twister

    • 난수 발생 속도가 빠르고 메모리를 적게 차지한다.

    • 난수 발생 주기(똑같은 숫자가 발생하는 주기)가 메르센 소수인 2^19937 - 1이라서 이름이 mt19937이다.

    • 참고 : 케이플러스

    #include <iostream>
    #include <random>
    
    int main()
    {
        using namespace std;
    
        random_device   rd;
        mt19937         mersenne(rd());
        uniform_int_distribution<> dice(1, 6);
    
        for (int i = 0; i < 10; ++i)
            cout << dice(mersenne) << endl;
    }
저작자표시

'C++ > Library' 카테고리의 다른 글

C++ cin.ignore  (0) 2021.03.11
C++ cin  (0) 2021.03.11
C++ 입력 버퍼 무시하기  (0) 2021.03.10
C++ typeinfo  (0) 2021.03.10
C++ 출력 버퍼 비우기  (0) 2021.03.10
'C++/Library' 카테고리의 다른 글
  • C++ cin.ignore
  • C++ cin
  • C++ 입력 버퍼 무시하기
  • C++ typeinfo
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (316)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (19)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    윈도우 명령어
    그림판
    윈도우
    스프링 프레임워크 핵심 기술
    EXCLUDE
    MacOS
    unix
    mspaint
    시스템 복구
    KakaoTalk
    스프링
    java
    로지텍 마우스 제스처
    제외
    알림
    windows
    Windows 11
    백기선
    logi options
    SFC
    citrix workspace
    spring
    Solaris 10
    SunOS 5.1
    dism
    맥북 카카오톡 알림 안뜸
    Workspace
    vscode
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ 난수 생성 (Random Number Generation)
상단으로

티스토리툴바