1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <random>
10
11// template<class IntType = int>
12// class discrete_distribution
13
14// template<class UnaryOperation>
15// discrete_distribution(size_t nw, double xmin, double xmax,
16// UnaryOperation fw);
17
18#include <random>
19
20#include <cassert>
21#include <vector>
22
23#include "test_macros.h"
24
25double fw(double x)
26{
27 return x+1;
28}
29
30int main(int, char**)
31{
32 {
33 typedef std::discrete_distribution<> D;
34 D d(0, 0, 1, fw);
35 std::vector<double> p = d.probabilities();
36 assert(p.size() == 1);
37 assert(p[0] == 1);
38 }
39 {
40 typedef std::discrete_distribution<> D;
41 D d(1, 0, 1, fw);
42 std::vector<double> p = d.probabilities();
43 assert(p.size() == 1);
44 assert(p[0] == 1);
45 }
46 {
47 typedef std::discrete_distribution<> D;
48 D d(2, 0.5, 1.5, fw);
49 std::vector<double> p = d.probabilities();
50 assert(p.size() == 2);
51 assert(p[0] == .4375);
52 assert(p[1] == .5625);
53 }
54 {
55 typedef std::discrete_distribution<> D;
56 D d(4, 0, 2, fw);
57 std::vector<double> p = d.probabilities();
58 assert(p.size() == 4);
59 assert(p[0] == .15625);
60 assert(p[1] == .21875);
61 assert(p[2] == .28125);
62 }
63
64 return 0;
65}
66

source code of libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/ctor_func.pass.cpp