piecewise_linear_distribution — Klasa

Generuje rozkład liniowy fragmentarycznie, który ma różne interwały szerokości z liniowo różnym prawdopodobieństwem w każdym interwale.

Składnia

template<class RealType = double>
class piecewise_linear_distribution
   {
public:
   // types
   typedef RealType result_type;
   struct param_type;

   // constructor and reset functions
   piecewise_linear_distribution();
   template <class InputIteratorI, class InputIteratorW>
   piecewise_linear_distribution(
      InputIteratorI firstI, InputIteratorI lastI, InputIteratorW firstW);
   template <class UnaryOperation>
   piecewise_linear_distribution(
      initializer_list<result_type> intervals, UnaryOperation weightfunc);
   template <class UnaryOperation>
   piecewise_linear_distribution(
      size_t count, result_type xmin, result_type xmax, UnaryOperation weightfunc);
   explicit piecewise_linear_distribution(const param_type& parm);
   void reset();

   // generating functions
   template <class URNG>
   result_type operator()(URNG& gen);
   template <class URNG>
   result_type operator()(URNG& gen, const param_type& parm);

   // property functions
   vector<result_type> intervals() const;
   vector<result_type> densities() const;
   param_type param() const;
   void param(const param_type& parm);
   result_type min() const;
   result_type max() const;
   };

Parametry

Typ rzeczywisty
Typ wyniku zmiennoprzecinkowego domyślnie to double. Aby uzyskać informacje o możliwych typach, zobacz losowe>.<

Uwagi

Ten rozkład próbkowania ma różne interwały szerokości z liniowo różnym prawdopodobieństwem w każdym interwale. Aby uzyskać informacje o innych dystrybucjach próbkowania, zobacz piecewise_linear_distribution i discrete_distribution.

Poniższa tabela zawiera linki do artykułów dotyczących poszczególnych członków:

piecewise_linear_distribution
param_type

Funkcja właściwości zwraca vector<result_type> element intervals() z zestawem przechowywanych interwałów rozkładu.

Funkcja densities() właściwości zwraca vector<result_type> wartość z przechowywanymi gęstościami dla każdego zestawu interwałów, które są obliczane zgodnie z wagami podanymi w parametrach konstruktora.

Składowa param() właściwości ustawia lub zwraca przechowywany pakiet parametrów param_type dystrybucji.

Funkcje min() składowe i max() zwracają najmniejszy możliwy wynik i największy możliwy wynik, odpowiednio.

reset() Funkcja składowa odrzuca wszystkie buforowane wartości, dzięki czemu wynik następnego wywołania operator() nie zależy od żadnych wartości uzyskanych z aparatu przed wywołaniem.

operator() Funkcje składowe zwracają następną wygenerowaną wartość na podstawie aparatu URNG z bieżącego pakietu parametrów lub określonego pakietu parametrów.

Aby uzyskać więcej informacji na temat klas dystrybucji i ich składowych, zobacz losowe>.<

Przykład

// compile with: /EHsc /W4
#include <random>
#include <iostream>
#include <iomanip>
#include <string>
#include <map>

using namespace std;

void test(const int s) {

    // uncomment to use a non-deterministic generator
    // random_device rd;
    // mt19937 gen(rd());
    mt19937 gen(1701);

    // Three intervals, non-uniform: 0 to 1, 1 to 6, and 6 to 15
    vector<double> intervals{ 0, 1, 6, 15 };
    // weights determine the densities used by the distribution
    vector<double> weights{ 1, 5, 5, 10 };

    piecewise_linear_distribution<double> distr(intervals.begin(), intervals.end(), weights.begin());

    cout << endl;
    cout << "min() == " << distr.min() << endl;
    cout << "max() == " << distr.max() << endl;
    cout << "intervals (index: interval):" << endl;
    vector<double> i = distr.intervals();
    int counter = 0;
    for (const auto& n : i) {
        cout << fixed << setw(11) << counter << ": " << setw(14) << setprecision(10) << n << endl;
        ++counter;
    }
    cout << endl;
    cout << "densities (index: density):" << endl;
    vector<double> d = distr.densities();
    counter = 0;
    for (const auto& n : d) {
        cout << fixed << setw(11) << counter << ": " << setw(14) << setprecision(10) << n << endl;
        ++counter;
    }
    cout << endl;

    // generate the distribution as a histogram
    map<int, int> histogram;
    for (int i = 0; i < s; ++i) {
        ++histogram[distr(gen)];
    }

    // print results
    cout << "Distribution for " << s << " samples:" << endl;
    for (const auto& elem : histogram) {
        cout << setw(5) << elem.first << '-' << elem.first + 1 << ' ' << string(elem.second, ':') << endl;
    }
    cout << endl;
}

int main()
{
    int samples = 100;

    cout << "Use CTRL-Z to bypass data entry and run using default values." << endl;
    cout << "Enter an integer value for the sample count: ";
    cin >> samples;

    test(samples);
}
Use CTRL-Z to bypass data entry and run using default values.
Enter an integer value for the sample count: 100
min() == 0
max() == 15
intervals (index: interval):
          0:   0.0000000000
          1:   1.0000000000
          2:   6.0000000000
          3:  15.0000000000
densities (index: density):
          0:   0.0645161290
          1:   0.3225806452
          2:   0.3225806452
          3:   0.6451612903
Distribution for 100 samples:
    0-1 :::::::::::::::::::::
    1-2 ::::::
    2-3 :::
    3-4 :::::::
    4-5 ::::::
    5-6 ::::::
    6-7 :::::
    7-8 ::::::::::
    8-9 ::::::::::
    9-10 ::::::
   10-11 ::::
   11-12 :::
   12-13 :::
   13-14 :::::
   14-15 :::::

Wymagania

Nagłówek:<losowy>

Przestrzeń nazw: std

piecewise_linear_distribution::p iecewise_linear_distribution

Tworzy rozkład.

// default constructor
piecewise_linear_distribution();

// constructs using a range of intervals, [firstI, lastI), with
// matching weights starting at firstW
template <class InputIteratorI, class InputIteratorW>
piecewise_linear_distribution(InputIteratorI firstI, InputIteratorI lastI, InputIteratorW firstW);

// constructs using an initializer list for range of intervals,
// with weights generated by function weightfunc
template <class UnaryOperation>
piecewise_linear_distribution(initializer_list<RealType>
intervals, UnaryOperation weightfunc);

// constructs using an initializer list for range of count intervals,
// distributed uniformly over [xmin,xmax] with weights generated by function weightfunc
template <class UnaryOperation>
piecewise_linear_distribution(size_t count, RealType xmin, RealType xmax, UnaryOperation weightfunc);

// constructs from an existing param_type structure
explicit piecewise_linear_distribution(const param_type& parm);

Parametry

firstI
Iterator wejściowy pierwszego elementu w zakresie dystrybucji.

lastI
Iterator wejściowy ostatniego elementu w zakresie dystrybucji.

firstW
Iterator wejściowy pierwszego elementu w zakresie wag.

Odstępach czasu
Initializer_list z interwałami rozkładu.

count
Liczba elementów w zakresie dystrybucji.

xmin
Najniższa wartość w zakresie dystrybucji.

Xmax
Najwyższa wartość w zakresie dystrybucji. Musi być większy niż xmin.

weightfunc
Obiekt reprezentujący funkcję prawdopodobieństwa dla rozkładu. Zarówno parametr, jak i wartość zwracana muszą być konwertowane na doublewartość .

Parm
Struktura parametrów używana do konstruowania rozkładu.

Uwagi

Domyślny konstruktor ustawia przechowywane parametry, tak aby istniał jeden interwał, od 0 do 1, z gęstością prawdopodobieństwa wynoszącą 1.

Konstruktor zakresu iteratora

template <class InputIteratorI, class InputIteratorW>
piecewise_linear_distribution(
    InputIteratorI firstI,
    InputIteratorI lastI,
    InputIteratorW firstW);

tworzy obiekt rozkładu z iteratorami z iteratorów w sekwencji [ firstI, lastI) i pasującą sekwencję wagi rozpoczynającą się od początkuW.

Konstruktor listy inicjatora

template <class UnaryOperation>
piecewise_linear_distribution(
    initializer_list<result_type> intervals,
    UnaryOperation weightfunc);

tworzy obiekt rozkładu z interwałami z interwałów listy inicjatora i wag generowanych na podstawie funkcji weightfunc.

Konstruktor zdefiniowany jako

template <class UnaryOperation>
piecewise_linear_distribution(
    size_t count,
    result_type xmin,
    result_type xmax,
    UnaryOperation weightfunc);

tworzy obiekt rozkładu z interwałami liczbowymi równomiernie rozłożonymi na [ xmin,xmax], przypisując każdą wagę interwału zgodnie z wagą funkcji, a funkcja weightfunc musi zaakceptować jeden parametr i mieć wartość zwracaną, z których obie są konwertowane na doublewartość . Warunek wstępny:xmin < xmax

Konstruktor zdefiniowany jako

explicit piecewise_linear_distribution(const param_type& parm);

tworzy obiekt dystrybucji przy użyciu parm jako przechowywanej struktury parametrów.

piecewise_linear_distribution::p aram_type

Przechowuje wszystkie parametry dystrybucji.

struct param_type {
   typedef piecewise_linear_distribution<result_type> distribution_type;
   param_type();
   template <class IterI, class IterW>
   param_type(
      IterI firstI, IterI lastI, IterW firstW);
   template <class UnaryOperation>
   param_type(
      size_t count, result_type xmin, result_type xmax, UnaryOperation weightfunc);
   std::vector<result_type> densities() const;
   std::vector<result_type> intervals() const;

   bool operator==(const param_type& right) const;
   bool operator!=(const param_type& right) const;
   };

Parametry

Zobacz parametry konstruktora dla piecewise_linear_distribution.

Uwagi

Warunek wstępny:xmin < xmax

Tę strukturę można przekazać do konstruktora klasy rozkładu podczas tworzenia wystąpienia, do param() funkcji składowej w celu ustawienia przechowywanych parametrów istniejącej dystrybucji i operator() do użycia zamiast przechowywanych parametrów.

Zobacz też

<Losowe>