[rand.dist.samp.discrete]

llvm-svn: 104103
This commit is contained in:
Howard Hinnant 2010-05-19 01:53:57 +00:00
parent 2c452fcd14
commit fb0e5ec825
25 changed files with 1602 additions and 3 deletions

View File

@ -1410,7 +1410,71 @@ public:
};
template<class IntType = int>
class discrete_distribution;
class discrete_distribution
{
public:
// types
typedef IntType result_type;
class param_type
{
public:
typedef discrete_distribution distribution_type;
param_type();
template<class InputIterator>
param_type(InputIterator firstW, InputIterator lastW);
param_type(initializer_list<double> wl);
template<class UnaryOperation>
param_type(size_t nw, double xmin, double xmax, UnaryOperation fw);
vector<double> probabilities() const;
friend bool operator==(const param_type& x, const param_type& y);
friend bool operator!=(const param_type& x, const param_type& y);
};
// constructor and reset functions
discrete_distribution();
template<class InputIterator>
discrete_distribution(InputIterator firstW, InputIterator lastW);
discrete_distribution(initializer_list<double> wl);
template<class UnaryOperation>
discrete_distribution(size_t nw, double xmin, double xmax,
UnaryOperation fw);
explicit discrete_distribution(const param_type& parm);
void reset();
// generating functions
template<class URNG> result_type operator()(URNG& g);
template<class URNG> result_type operator()(URNG& g, const param_type& parm);
// property functions
vector<double> probabilities() const;
param_type param() const;
void param(const param_type& parm);
result_type min() const;
result_type max() const;
friend bool operator==(const discrete_distribution& x,
const discrete_distribution& y);
friend bool operator!=(const discrete_distribution& x,
const discrete_distribution& y);
template <class charT, class traits>
friend
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
const discrete_distribution& x);
template <class charT, class traits>
friend
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
discrete_distribution& x);
};
template<class RealType = double>
class piecewise_constant_distribution;
@ -1428,6 +1492,7 @@ template<class RealType = double>
#include <cstdint>
#include <limits>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <istream>
@ -4075,7 +4140,7 @@ public:
result_type s() const {return __p_.s();}
param_type param() const {return __p_;}
void param(const param_type& __p) {return __p_ = __p;}
void param(const param_type& __p) {__p_ = __p;}
result_type min() const {return 0;}
result_type max() const {return numeric_limits<result_type>::infinity();}
@ -5176,6 +5241,8 @@ operator>>(basic_istream<_CharT, _Traits>& __is,
return __is;
}
// student_t_distribution
template<class _RealType = double>
class student_t_distribution
{
@ -5220,7 +5287,7 @@ public:
result_type n() const {return __p_.n();}
param_type param() const {return __p_;}
void param(const param_type& __p) {return __p_ = __p;}
void param(const param_type& __p) {__p_ = __p;}
result_type min() const {return -numeric_limits<result_type>::infinity();}
result_type max() const {return numeric_limits<result_type>::infinity();}
@ -5270,6 +5337,212 @@ operator>>(basic_istream<_CharT, _Traits>& __is,
return __is;
}
// discrete_distribution
template<class _IntType = int>
class discrete_distribution
{
public:
// types
typedef _IntType result_type;
class param_type
{
vector<double> __p_;
public:
typedef discrete_distribution distribution_type;
param_type() {}
template<class _InputIterator>
param_type(_InputIterator __f, _InputIterator __l)
: __p_(__f, __l) {__init();}
param_type(initializer_list<double> __wl)
: __p_(__wl.begin(), __wl.end()) {__init();}
template<class _UnaryOperation>
param_type(size_t __nw, double __xmin, double __xmax,
_UnaryOperation __fw);
vector<double> probabilities() const;
friend bool operator==(const param_type& __x, const param_type& __y)
{return __x.__p_ == __y.__p_;}
friend bool operator!=(const param_type& __x, const param_type& __y)
{return !(__x == __y);}
private:
void __init();
friend class discrete_distribution;
template <class _CharT, class _Traits, class _IT>
friend
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
const discrete_distribution<_IT>& __x);
template <class _CharT, class _Traits, class _IT>
friend
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
discrete_distribution<_IT>& __x);
};
private:
param_type __p_;
public:
// constructor and reset functions
discrete_distribution() {}
template<class _InputIterator>
discrete_distribution(_InputIterator __f, _InputIterator __l)
: __p_(__f, __l) {}
discrete_distribution(initializer_list<double> __wl)
: __p_(__wl) {}
template<class _UnaryOperation>
discrete_distribution(size_t __nw, double __xmin, double __xmax,
_UnaryOperation __fw)
: __p_(__nw, __xmin, __xmax, __fw) {}
explicit discrete_distribution(const param_type& __p)
: __p_(__p) {}
void reset() {}
// generating functions
template<class _URNG> result_type operator()(_URNG& __g)
{return (*this)(__g, __p_);}
template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
// property functions
vector<double> probabilities() const {return __p_.probabilities();}
param_type param() const {return __p_;}
void param(const param_type& __p) {__p_ = __p;}
result_type min() const {return 0;}
result_type max() const {return __p_.__p_.size();}
friend bool operator==(const discrete_distribution& __x,
const discrete_distribution& __y)
{return __x.__p_ == __y.__p_;}
friend bool operator!=(const discrete_distribution& __x,
const discrete_distribution& __y)
{return !(__x == __y);}
template <class _CharT, class _Traits, class _IT>
friend
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
const discrete_distribution<_IT>& __x);
template <class _CharT, class _Traits, class _IT>
friend
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
discrete_distribution<_IT>& __x);
};
template<class _IntType>
template<class _UnaryOperation>
discrete_distribution<_IntType>::param_type::param_type(size_t __nw,
double __xmin,
double __xmax,
_UnaryOperation __fw)
{
if (__nw > 1)
{
__p_.reserve(__nw - 1);
double __d = (__xmax - __xmin) / __nw;
double __d2 = __d / 2;
for (size_t __k = 0; __k < __nw; ++__k)
__p_.push_back(__fw(__xmin + __k * __d + __d2));
__init();
}
}
template<class _IntType>
void
discrete_distribution<_IntType>::param_type::__init()
{
if (!__p_.empty())
{
if (__p_.size() > 1)
{
double __s = _STD::accumulate(__p_.begin(), __p_.end(), 0.0);
for (_STD::vector<double>::iterator __i = __p_.begin(), __e = __p_.end();
__i < __e; ++__i)
*__i /= __s;
vector<double> __t(__p_.size() - 1);
_STD::partial_sum(__p_.begin(), __p_.end() - 1, __t.begin());
swap(__p_, __t);
}
else
{
__p_.clear();
__p_.shrink_to_fit();
}
}
}
template<class _IntType>
vector<double>
discrete_distribution<_IntType>::param_type::probabilities() const
{
size_t __n = __p_.size();
_STD::vector<double> __p(__n+1);
_STD::adjacent_difference(__p_.begin(), __p_.end(), __p.begin());
if (__n > 0)
__p[__n] = 1 - __p_[__n-1];
else
__p[0] = 1;
return __p;
}
template<class _IntType>
template<class _URNG>
_IntType
discrete_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p)
{
uniform_real_distribution<double> __gen;
return static_cast<_IntType>(
_STD::upper_bound(__p.__p_.begin(), __p.__p_.end(), __gen(__g)) -
__p.__p_.begin());
}
template <class _CharT, class _Traits, class _IT>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
const discrete_distribution<_IT>& __x)
{
__save_flags<_CharT, _Traits> _(__os);
__os.flags(ios_base::dec | ios_base::left);
_CharT __sp = __os.widen(' ');
__os.fill(__sp);
size_t __n = __x.__p_.__p_.size();
__os << __n;
for (size_t __i = 0; __i < __n; ++__i)
__os << __sp << __x.__p_.__p_[__i];
return __os;
}
template <class _CharT, class _Traits, class _IT>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
discrete_distribution<_IT>& __x)
{
typedef discrete_distribution<_IT> _Eng;
typedef typename _Eng::result_type result_type;
typedef typename _Eng::param_type param_type;
__save_flags<_CharT, _Traits> _(__is);
__is.flags(ios_base::dec | ios_base::skipws);
size_t __n;
__is >> __n;
std::vector<double> __p(__n);
for (size_t __i = 0; __i < __n; ++__i)
__is >> __p[__i];
if (!__is.fail())
swap(__x.__p_.__p_, __p);
return __is;
}
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_RANDOM

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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), "");
}
}

View File

@ -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);
}
}

View File

@ -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), "");
}
}