42 |
26 Feb 04 |
jari |
// $Id$ |
42 |
26 Feb 04 |
jari |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
831 |
27 Mar 07 |
peter |
Copyright (C) 2003 Daniel Dalevi |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2004 Jari Häkkinen |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2005 Jari Häkkinen, Peter Johansson |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
4207 |
26 Aug 22 |
peter |
Copyright (C) 2012, 2015, 2016, 2020, 2022 Peter Johansson |
377 |
07 Aug 05 |
jari |
10 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
675 |
10 Oct 06 |
jari |
12 |
|
675 |
10 Oct 06 |
jari |
The yat library is free software; you can redistribute it and/or |
675 |
10 Oct 06 |
jari |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
675 |
10 Oct 06 |
jari |
License, or (at your option) any later version. |
675 |
10 Oct 06 |
jari |
17 |
|
675 |
10 Oct 06 |
jari |
The yat library is distributed in the hope that it will be useful, |
675 |
10 Oct 06 |
jari |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
675 |
10 Oct 06 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
675 |
10 Oct 06 |
jari |
General Public License for more details. |
675 |
10 Oct 06 |
jari |
22 |
|
675 |
10 Oct 06 |
jari |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
675 |
10 Oct 06 |
jari |
25 |
*/ |
675 |
10 Oct 06 |
jari |
26 |
|
2881 |
18 Nov 12 |
peter |
27 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
28 |
|
1245 |
17 Mar 08 |
peter |
29 |
#include "Suite.h" |
1245 |
17 Mar 08 |
peter |
30 |
|
675 |
10 Oct 06 |
jari |
31 |
#include "yat/random/random.h" |
798 |
12 Mar 07 |
jari |
32 |
#include "yat/statistics/Histogram.h" |
675 |
10 Oct 06 |
jari |
33 |
|
13 |
19 Jun 03 |
daniel |
34 |
#include <iostream> |
4384 |
13 Oct 23 |
peter |
35 |
#include <list> |
379 |
08 Aug 05 |
peter |
36 |
#include <sstream> |
379 |
08 Aug 05 |
peter |
37 |
#include <fstream> |
13 |
19 Jun 03 |
daniel |
38 |
|
4382 |
13 Oct 23 |
peter |
39 |
void test_discrete_general(theplu::yat::test::Suite& suite) |
4382 |
13 Oct 23 |
peter |
40 |
{ |
4382 |
13 Oct 23 |
peter |
41 |
std::cerr << __func__ << "\n"; |
4382 |
13 Oct 23 |
peter |
42 |
size_t N = 100; |
4382 |
13 Oct 23 |
peter |
43 |
theplu::yat::statistics::Histogram histogram(0, N, N); |
4382 |
13 Oct 23 |
peter |
44 |
for (size_t i=0; i<N; ++i) { |
4382 |
13 Oct 23 |
peter |
45 |
histogram.add(i, i*i); |
4382 |
13 Oct 23 |
peter |
46 |
} |
4382 |
13 Oct 23 |
peter |
47 |
|
4382 |
13 Oct 23 |
peter |
48 |
theplu::yat::random::DiscreteGeneral rng; |
4382 |
13 Oct 23 |
peter |
49 |
{ |
4382 |
13 Oct 23 |
peter |
50 |
theplu::yat::random::DiscreteGeneral rng2(histogram); |
4382 |
13 Oct 23 |
peter |
51 |
for (size_t i=0; i<10; ++i) |
4382 |
13 Oct 23 |
peter |
52 |
suite.out() << i << " : " << rng2() << "\n"; |
4382 |
13 Oct 23 |
peter |
53 |
rng = std::move(rng2); |
4382 |
13 Oct 23 |
peter |
54 |
} |
4382 |
13 Oct 23 |
peter |
55 |
for (size_t i=0; i<10; ++i) |
4382 |
13 Oct 23 |
peter |
56 |
suite.out() << i << " : " << rng() << "\n"; |
4384 |
13 Oct 23 |
peter |
57 |
|
4384 |
13 Oct 23 |
peter |
58 |
|
4384 |
13 Oct 23 |
peter |
59 |
if (false) { // avoid running compilation tests |
4384 |
13 Oct 23 |
peter |
60 |
boost::input_iterator_archetype<double> iter; |
4384 |
13 Oct 23 |
peter |
61 |
theplu::yat::random::DiscreteGeneral rng3(iter, iter); |
4384 |
13 Oct 23 |
peter |
62 |
rng3(); |
4384 |
13 Oct 23 |
peter |
63 |
} |
4384 |
13 Oct 23 |
peter |
64 |
std::list<int> data = {1, 2, 6, 1, 1}; |
4384 |
13 Oct 23 |
peter |
65 |
theplu::yat::random::DiscreteGeneral rng4(data.cbegin(), data.cend()); |
4384 |
13 Oct 23 |
peter |
66 |
size_t k = rng4(); |
4384 |
13 Oct 23 |
peter |
67 |
suite.out() << "k: " << k << "\n"; |
4382 |
13 Oct 23 |
peter |
68 |
} |
4382 |
13 Oct 23 |
peter |
69 |
|
4382 |
13 Oct 23 |
peter |
70 |
|
1245 |
17 Mar 08 |
peter |
71 |
int main(int argc, char* argv[]) |
13 |
19 Jun 03 |
daniel |
72 |
{ |
798 |
12 Mar 07 |
jari |
73 |
using namespace theplu::yat::random; |
1245 |
17 Mar 08 |
peter |
74 |
theplu::yat::test::Suite suite(argc, argv); |
1245 |
17 Mar 08 |
peter |
75 |
suite.err() << "testing rnd" << std::endl; |
379 |
08 Aug 05 |
peter |
76 |
|
798 |
12 Mar 07 |
jari |
77 |
RNG* rng=RNG::instance(); |
798 |
12 Mar 07 |
jari |
78 |
rng->seed_from_devurandom(); |
798 |
12 Mar 07 |
jari |
79 |
|
425 |
07 Dec 05 |
jari |
// testing that minimal integer is zero for the generator |
1245 |
17 Mar 08 |
peter |
81 |
suite.err() << "Checking that RNG minimum value is zero" << std::endl; |
379 |
08 Aug 05 |
peter |
82 |
if (rng->min()){ |
1245 |
17 Mar 08 |
peter |
83 |
suite.err() << "Error: rng->min is not zero" << std::endl; |
1245 |
17 Mar 08 |
peter |
84 |
suite.add(false); |
379 |
08 Aug 05 |
peter |
85 |
} |
379 |
08 Aug 05 |
peter |
86 |
|
1610 |
04 Nov 08 |
peter |
87 |
suite.err() << "Checking that all random generators can be constructed\n"; |
798 |
12 Mar 07 |
jari |
88 |
theplu::yat::statistics::Histogram histogram(0,100,1000); |
4009 |
19 Oct 20 |
peter |
89 |
std::vector<DiscreteGeneral> discrete(10); |
798 |
12 Mar 07 |
jari |
90 |
DiscreteGeneral dg(histogram); |
798 |
12 Mar 07 |
jari |
91 |
DiscreteUniform du; |
3893 |
27 Mar 20 |
peter |
92 |
{ |
3893 |
27 Mar 20 |
peter |
93 |
DiscreteUniform::result_type x = 0; |
3893 |
27 Mar 20 |
peter |
94 |
theplu::yat::test::avoid_compiler_warning(x); |
3893 |
27 Mar 20 |
peter |
95 |
DiscreteUniform::argument_type y = 0; |
3893 |
27 Mar 20 |
peter |
96 |
theplu::yat::test::avoid_compiler_warning(y); |
3893 |
27 Mar 20 |
peter |
97 |
} |
798 |
12 Mar 07 |
jari |
98 |
Poisson p; |
798 |
12 Mar 07 |
jari |
99 |
ContinuousUniform cu; |
798 |
12 Mar 07 |
jari |
100 |
ContinuousGeneral cg(histogram); |
798 |
12 Mar 07 |
jari |
101 |
Exponential e; |
798 |
12 Mar 07 |
jari |
102 |
Gaussian g; |
379 |
08 Aug 05 |
peter |
103 |
|
2889 |
07 Dec 12 |
peter |
104 |
Binomial bin(0.5, 10); |
3446 |
02 Dec 15 |
peter |
105 |
Geometric geo(0.5); |
3446 |
02 Dec 15 |
peter |
106 |
geo(); |
3446 |
02 Dec 15 |
peter |
107 |
geo(0.1); |
3465 |
10 Feb 16 |
peter |
108 |
HyperGeometric hg; |
3465 |
10 Feb 16 |
peter |
109 |
hg(10, 100, 4); |
3465 |
10 Feb 16 |
peter |
110 |
HyperGeometric hg2(10, 100, 4); |
3465 |
10 Feb 16 |
peter |
111 |
hg2(); |
3465 |
10 Feb 16 |
peter |
112 |
hg2(11, 100, 4); |
2889 |
07 Dec 12 |
peter |
113 |
|
3469 |
29 Feb 16 |
peter |
114 |
NegativeHyperGeometric nhg; |
3469 |
29 Feb 16 |
peter |
115 |
nhg(10, 100, 4); |
3469 |
29 Feb 16 |
peter |
116 |
HyperGeometric nhg2(10, 100, 4); |
3469 |
29 Feb 16 |
peter |
117 |
nhg2(); |
3469 |
29 Feb 16 |
peter |
// test that symmetric case doesn't get stuck |
3469 |
29 Feb 16 |
peter |
119 |
nhg2(11, 100, 6); |
3469 |
29 Feb 16 |
peter |
120 |
|
4147 |
28 Feb 22 |
peter |
121 |
NegativeBinomial nb(0.5, 12); |
4147 |
28 Feb 22 |
peter |
122 |
|
4382 |
13 Oct 23 |
peter |
123 |
test_discrete_general(suite); |
1245 |
17 Mar 08 |
peter |
124 |
return suite.return_value(); |
13 |
19 Jun 03 |
daniel |
125 |
} |