[rand.dist.samp.discrete]
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@104103 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// discrete_distribution& operator=(const discrete_distribution&);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
void
|
||||
test1()
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p[] = {2, 4, 1, 8};
|
||||
D d1(p, p+4);
|
||||
D d2;
|
||||
assert(d1 != d2);
|
||||
d2 = d1;
|
||||
assert(d1 == d2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test1();
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// discrete_distribution(const discrete_distribution&);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
void
|
||||
test1()
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p[] = {2, 4, 1, 8};
|
||||
D d1(p, p+4);
|
||||
D d2 = d1;
|
||||
assert(d1 == d2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test1();
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// discrete_distribution(initializer_list<double> wl);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d;
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// template<class UnaryOperation>
|
||||
// discrete_distribution(size_t nw, double xmin, double xmax,
|
||||
// UnaryOperation fw);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
double fw(double x)
|
||||
{
|
||||
return x+1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d(0, 0, 1, fw);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d(1, 0, 1, fw);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d(2, 0.5, 1.5, fw);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == .4375);
|
||||
assert(p[1] == .5625);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d(4, 0, 2, fw);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 4);
|
||||
assert(p[0] == .15625);
|
||||
assert(p[1] == .21875);
|
||||
assert(p[2] == .28125);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// discrete_distribution(initializer_list<double> wl);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
#ifdef _LIBCPP_MOVE
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d = {};
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d = {10};
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d = {10, 30};
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.25);
|
||||
assert(p[1] == 0.75);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d = {30, 10};
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.75);
|
||||
assert(p[1] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d = {30, 0, 10};
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0.75);
|
||||
assert(p[1] == 0);
|
||||
assert(p[2] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d = {0, 30, 10};
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0);
|
||||
assert(p[1] == 0.75);
|
||||
assert(p[2] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d = {0, 0, 10};
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0);
|
||||
assert(p[1] == 0);
|
||||
assert(p[2] == 1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// template<class InputIterator>
|
||||
// discrete_distribution(InputIterator firstW, InputIterator lastW);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {1};
|
||||
D d(p0, p0);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {10};
|
||||
D d(p0, p0+1);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {10, 30};
|
||||
D d(p0, p0+2);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.25);
|
||||
assert(p[1] == 0.75);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {30, 10};
|
||||
D d(p0, p0+2);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.75);
|
||||
assert(p[1] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {30, 0, 10};
|
||||
D d(p0, p0+3);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0.75);
|
||||
assert(p[1] == 0);
|
||||
assert(p[2] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {0, 30, 10};
|
||||
D d(p0, p0+3);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0);
|
||||
assert(p[1] == 0.75);
|
||||
assert(p[2] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {0, 0, 10};
|
||||
D d(p0, p0+3);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0);
|
||||
assert(p[1] == 0);
|
||||
assert(p[2] == 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// explicit discrete_distribution(const param_type& parm);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {10, 30};
|
||||
P pa(p0, p0+2);
|
||||
D d(pa);
|
||||
std::vector<double> p = d.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.25);
|
||||
assert(p[1] == 0.75);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// bool operator=(const discrete_distribution& x,
|
||||
// const discrete_distribution& y);
|
||||
// bool operator!(const discrete_distribution& x,
|
||||
// const discrete_distribution& y);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
D d1;
|
||||
D d2;
|
||||
assert(d1 == d2);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {1};
|
||||
D d1(p0, p0+1);
|
||||
D d2;
|
||||
assert(d1 == d2);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {10, 30};
|
||||
D d1(p0, p0+2);
|
||||
D d2;
|
||||
assert(d1 != d2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// template<class _URNG> result_type operator()(_URNG& g);
|
||||
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
D d;
|
||||
const int N = 100;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
assert((double)u[i]/N == prob[i]);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {.3};
|
||||
D d(p0, p0+1);
|
||||
const int N = 100;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
assert((double)u[i]/N == prob[i]);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {.75, .25};
|
||||
D d(p0, p0+2);
|
||||
const int N = 1000000;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {0, 1};
|
||||
D d(p0, p0+2);
|
||||
const int N = 1000000;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
assert((double)u[0]/N == prob[0]);
|
||||
assert((double)u[1]/N == prob[1]);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {1, 0};
|
||||
D d(p0, p0+2);
|
||||
const int N = 1000000;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
assert((double)u[0]/N == prob[0]);
|
||||
assert((double)u[1]/N == prob[1]);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {.3, .1, .6};
|
||||
D d(p0, p0+3);
|
||||
const int N = 10000000;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {0, 25, 75};
|
||||
D d(p0, p0+3);
|
||||
const int N = 1000000;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
if (prob[i] != 0)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
else
|
||||
assert(u[i] == 0);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {25, 0, 75};
|
||||
D d(p0, p0+3);
|
||||
const int N = 1000000;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
if (prob[i] != 0)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
else
|
||||
assert(u[i] == 0);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {25, 75, 0};
|
||||
D d(p0, p0+3);
|
||||
const int N = 1000000;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
if (prob[i] != 0)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
else
|
||||
assert(u[i] == 0);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {0, 0, 1};
|
||||
D d(p0, p0+3);
|
||||
const int N = 100;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
if (prob[i] != 0)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
else
|
||||
assert(u[i] == 0);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {0, 1, 0};
|
||||
D d(p0, p0+3);
|
||||
const int N = 100;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
if (prob[i] != 0)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
else
|
||||
assert(u[i] == 0);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {1, 0, 0};
|
||||
D d(p0, p0+3);
|
||||
const int N = 100;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
if (prob[i] != 0)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
else
|
||||
assert(u[i] == 0);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
double p0[] = {33, 0, 0, 67};
|
||||
D d(p0, p0+3);
|
||||
const int N = 1000000;
|
||||
std::vector<D::result_type> u(d.max()+1);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g);
|
||||
assert(d.min() <= v && v <= d.max());
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = d.probabilities();
|
||||
for (int i = 0; i <= d.max(); ++i)
|
||||
if (prob[i] != 0)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
else
|
||||
assert(u[i] == 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// template<class _URNG> result_type operator()(_URNG& g, const param_type& parm);
|
||||
|
||||
#include <random>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
typedef std::minstd_rand G;
|
||||
G g;
|
||||
D d;
|
||||
double p0[] = {.3, .1, .6};
|
||||
P p(p0, p0+3);
|
||||
const int N = 10000000;
|
||||
std::vector<D::result_type> u(3);
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
D::result_type v = d(g, p);
|
||||
assert(0 <= v && v <= 2);
|
||||
u[v]++;
|
||||
}
|
||||
std::vector<double> prob = p.probabilities();
|
||||
for (int i = 0; i <= 2; ++i)
|
||||
assert(std::abs((double)u[i]/N - prob[i]) / prob[i] < 0.001);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// param_type param() const;
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {.3, .1, .6};
|
||||
P p(p0, p0+3);
|
||||
D d(p);
|
||||
assert(d.param() == p);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// template <class charT, class traits>
|
||||
// basic_ostream<charT, traits>&
|
||||
// operator<<(basic_ostream<charT, traits>& os,
|
||||
// const discrete_distribution& x);
|
||||
//
|
||||
// template <class charT, class traits>
|
||||
// basic_istream<charT, traits>&
|
||||
// operator>>(basic_istream<charT, traits>& is,
|
||||
// discrete_distribution& x);
|
||||
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {.3, .1, .6};
|
||||
D d1(p0, p0+3);
|
||||
std::ostringstream os;
|
||||
os << d1;
|
||||
std::istringstream is(os.str());
|
||||
D d2;
|
||||
is >> d2;
|
||||
assert(d1 == d2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// result_type max() const;
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {.3, .1, .6};
|
||||
D d(p0, p0+3);
|
||||
assert(d.max() == 2);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {.3, .1, .6, .2};
|
||||
D d(p0, p0+4);
|
||||
assert(d.max() == 3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// result_type min() const;
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
double p0[] = {.3, .1, .6};
|
||||
D d(p0, p0+3);
|
||||
assert(d.min() == 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
// {
|
||||
// class param_type;
|
||||
|
||||
#include <random>
|
||||
#include <limits>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type param_type;
|
||||
double d0[] = {.3, .1, .6};
|
||||
param_type p0(d0, d0+3);
|
||||
param_type p;
|
||||
p = p0;
|
||||
assert(p == p0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
// {
|
||||
// class param_type;
|
||||
|
||||
#include <random>
|
||||
#include <limits>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type param_type;
|
||||
double d0[] = {.3, .1, .6};
|
||||
param_type p0(d0, d0+3);
|
||||
param_type p = p0;
|
||||
assert(p == p0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// param_type(initializer_list<double> wl);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa;
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// template<class UnaryOperation>
|
||||
// param_type(size_t nw, double xmin, double xmax,
|
||||
// UnaryOperation fw);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
double fw(double x)
|
||||
{
|
||||
return x+1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa(0, 0, 1, fw);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa(1, 0, 1, fw);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa(2, 0.5, 1.5, fw);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == .4375);
|
||||
assert(p[1] == .5625);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa(4, 0, 2, fw);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 4);
|
||||
assert(p[0] == .15625);
|
||||
assert(p[1] == .21875);
|
||||
assert(p[2] == .28125);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// param_type(initializer_list<double> wl);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
#ifdef _LIBCPP_MOVE
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa = {};
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa = {10};
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa = {10, 30};
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.25);
|
||||
assert(p[1] == 0.75);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa = {30, 10};
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.75);
|
||||
assert(p[1] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa = {30, 0, 10};
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0.75);
|
||||
assert(p[1] == 0);
|
||||
assert(p[2] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa = {0, 30, 10};
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0);
|
||||
assert(p[1] == 0.75);
|
||||
assert(p[2] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
P pa = {0, 0, 10};
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0);
|
||||
assert(p[1] == 0);
|
||||
assert(p[2] == 1);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// template<class InputIterator>
|
||||
// param_type(InputIterator firstW, InputIterator lastW);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {1};
|
||||
P pa(p0, p0);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {10};
|
||||
P pa(p0, p0+1);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 1);
|
||||
assert(p[0] == 1);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {10, 30};
|
||||
P pa(p0, p0+2);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.25);
|
||||
assert(p[1] == 0.75);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {30, 10};
|
||||
P pa(p0, p0+2);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 2);
|
||||
assert(p[0] == 0.75);
|
||||
assert(p[1] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {30, 0, 10};
|
||||
P pa(p0, p0+3);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0.75);
|
||||
assert(p[1] == 0);
|
||||
assert(p[2] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {0, 30, 10};
|
||||
P pa(p0, p0+3);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0);
|
||||
assert(p[1] == 0.75);
|
||||
assert(p[2] == 0.25);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double p0[] = {0, 0, 10};
|
||||
P pa(p0, p0+3);
|
||||
std::vector<double> p = pa.probabilities();
|
||||
assert(p.size() == 3);
|
||||
assert(p[0] == 0);
|
||||
assert(p[1] == 0);
|
||||
assert(p[2] == 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
// {
|
||||
// class param_type;
|
||||
|
||||
#include <random>
|
||||
#include <limits>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type param_type;
|
||||
double p0[] = {30, 10};
|
||||
param_type p1(p0, p0+2);
|
||||
param_type p2(p0, p0+2);
|
||||
assert(p1 == p2);
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type param_type;
|
||||
double p0[] = {30, 10};
|
||||
param_type p1(p0, p0+2);
|
||||
param_type p2;
|
||||
assert(p1 != p2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
// {
|
||||
// class param_type;
|
||||
|
||||
#include <random>
|
||||
#include <type_traits>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type param_type;
|
||||
typedef param_type::distribution_type distribution_type;
|
||||
static_assert((std::is_same<D, distribution_type>::value), "");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
|
||||
// void param(const param_type& parm);
|
||||
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::param_type P;
|
||||
double d0[] = {.3, .1, .6};
|
||||
P p(d0, d0+3);
|
||||
D d;
|
||||
d.param(p);
|
||||
assert(d.param() == p);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// <random>
|
||||
|
||||
// template<class IntType = int>
|
||||
// class discrete_distribution
|
||||
// {
|
||||
// typedef bool result_type;
|
||||
|
||||
#include <random>
|
||||
#include <type_traits>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
typedef std::discrete_distribution<> D;
|
||||
typedef D::result_type result_type;
|
||||
static_assert((std::is_same<result_type, int>::value), "");
|
||||
}
|
||||
{
|
||||
typedef std::discrete_distribution<long> D;
|
||||
typedef D::result_type result_type;
|
||||
static_assert((std::is_same<result_type, long>::value), "");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user