Namespaces
Variants

std:: piecewise_constant_distribution

From cppreference.net
헤더 파일에 정의됨 <random>
template < class RealType = double >
class piecewise_constant_distribution ;
(C++11부터)

std::piecewise_constant_distribution 는 여러 하위 구간 [b i , b i+1 ) 각각 내에서 균일하게 분포된 임의의 부동 소수점 숫자를 생성합니다. 각 하위 구간은 고유의 가중치 w i 를 갖습니다. 구간 경계들의 집합과 가중치들의 집합이 이 분포의 매개변수입니다.

The probability density for any b i ≤ x <b i+1 is
w i
S (b i+1 - b i )
, where S is the sum of all weights.

std::piecewise_constant_distribution RandomNumberDistribution 의 모든 요구 사항을 충족합니다.

목차

템플릿 매개변수

RealType - 생성기에 의해 생성되는 결과 타입. 이것이 float , double , 또는 long double 중 하나가 아닌 경우의 효과는 정의되지 않습니다.

멤버 타입

멤버 타입 정의
result_type (C++11) RealType
param_type (C++11) 매개변수 집합의 타입, RandomNumberDistribution 참조.

멤버 함수

새로운 분포를 생성함
(public member function)
(C++11)
분포의 내부 상태를 재설정함
(public member function)
생성
(C++11)
분포에서 다음 난수를 생성함
(public member function)
특성
분포 매개변수를 반환함
(public member function)
(C++11)
분포 매개변수 객체를 가져오거나 설정함
(public member function)
(C++11)
잠재적으로 생성될 수 있는 최소값을 반환함
(public member function)
(C++11)
잠재적으로 생성될 수 있는 최대값을 반환함
(public member function)

비멤버 함수

(C++11) (C++11) (removed in C++20)
두 분포 객체를 비교함
(함수)
의사 난수 분포에 대한 스트림 입출력을 수행함
(함수 템플릿)

예제

#include <iostream>
#include <map>
#include <random>
#include <string>
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    // 50% 확률로 0과 1 사이의 난수 생성
    // 50% 확률로 10과 15 사이의 난수 생성
    std::vector<double> i {0, 1, 10, 15};
    std::vector<double> w {1, 0, 1};
    std::piecewise_constant_distribution<> d(i.begin(), i.end(), w.begin());
    std::map<int, int> hist;
    for (int n {}; n < 1e4; ++n)
        ++hist[d(gen)];
    for (std::cout << std::hex << std::uppercase; auto [x, y] : hist)
        std::cout << x << ' ' << std::string(y / 100, '*') << '\n';
}

가능한 출력:

0 **************************************************
A **********
B *********
C *********
D **********
E *********

참조문헌

  • C++23 표준 (ISO/IEC 14882:2024):
  • 28.5.9.6.2 클래스 템플릿 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 1421-1422)
  • C++20 표준(ISO/IEC 14882:2020):
  • 29.6.9.6.2 클래스 템플릿 piecewise_constant_distribution [rand.dist.samp.pconst] (페이지: 1207-1208)
  • C++17 표준(ISO/IEC 14882:2017):
  • 29.6.8.6.2 클래스 템플릿 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 1098-1100)
  • C++14 표준(ISO/IEC 14882:2014):
  • 26.5.8.6.2 클래스 템플릿 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 962-964)
  • C++11 표준(ISO/IEC 14882:2011):
  • 26.5.8.6.2 클래스 템플릿 piecewise_constant_distribution [rand.dist.samp.pconst] (p: 955-957)