std:: random_device
From cppreference.net
C++
Numerics library
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Pseudo-random number generation
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::random_device
| Member functions | ||||
| Generation | ||||
| Characteristics | ||||
|
헤더에 정의됨
<random>
|
||
|
class
random_device
;
|
(C++11부터) | |
std::random_device
는 균일하게 분포된 정수 난수 생성기로, 비결정적 난수를 생성합니다.
std::random_device
는 비결정적 소스(예: 하드웨어 장치)를 구현에서 사용할 수 없는 경우, 구현에서 정의된 의사 난수 엔진을 기반으로 구현될 수 있습니다. 이 경우 각
std::random_device
객체가 동일한 숫자 시퀀스를 생성할 수 있습니다.
목차 |
멤버 타입
| 멤버 타입 | 정의 |
result_type
(C++11)
|
unsigned int |
멤버 함수
Construction |
|
|
엔진을 생성함
(public member function) |
|
|
operator=
(deleted)
(C++11)
|
대입 연산자가 삭제됨
(public member function) |
Generation |
|
|
엔진의 상태를 진행시키고 생성된 값을 반환함
(public member function) |
|
Characteristics |
|
|
(C++11)
|
비결정적 난수 생성기의 엔트로피 추정값을 얻음
(public member function) |
|
[static]
|
출력 범위에서 가능한 가장 작은 값을 얻음
(public static member function) |
|
[static]
|
출력 범위에서 가능한 가장 큰 값을 얻음
(public static member function) |
참고 사항
MinGW-w64의 구버전에서
std::random_device
가 결정론적으로 동작했던 주목할 만한 구현 사례입니다 (
버그 338
, GCC 9.2 이후 수정됨). 최신 MinGW-w64 버전은
MCF 스레드 모델을 탑재한 GCC
에서 다운로드할 수 있습니다.
예제
이 코드 실행
#include <iostream> #include <map> #include <random> #include <string> int main() { std::random_device rd; std::map<int, int> hist; std::uniform_int_distribution<int> dist(0, 9); for (int n = 0; n != 20000; ++n) ++hist[dist(rd)]; // 참고: 데모 전용: 많은 구현에서 random_device의 성능은 // 엔트로피 풀이 고갈되면 급격히 저하됩니다. 실제 사용에서는 // random_device는 일반적으로 mt19937과 같은 // PRNG를 시드하는 데만 사용됩니다 for (auto [x, y] : hist) std::cout << x << " : " << std::string(y / 100, '*') << '\n'; }
가능한 출력:
0 : ******************** 1 : ******************* 2 : ******************** 3 : ******************** 4 : ******************** 5 : ******************* 6 : ******************** 7 : ******************** 8 : ******************* 9 : ********************