]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright John Maddock 2005. |
2 | // Use, modification and distribution are subject to the | |
3 | // Boost Software License, Version 1.0. (See accompanying file | |
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #ifdef TEST_STD_HEADERS | |
7 | #include <random> | |
8 | #else | |
9 | #include <boost/tr1/random.hpp> | |
10 | #endif | |
11 | ||
12 | #include <boost/test/test_tools.hpp> | |
13 | #include <boost/test/included/test_exec_monitor.hpp> | |
14 | ||
15 | #include <iostream> | |
16 | #include <iomanip> | |
17 | ||
18 | template<class PRNG, class R> | |
19 | void validate(const std::string & name, PRNG &rng, R expected) | |
20 | { | |
21 | typedef typename PRNG::result_type result_type; | |
22 | std::cout << "validating " << name << ": "; | |
23 | for(int i = 0; i < 9999; i++) | |
24 | { | |
25 | result_type r = rng(); | |
26 | BOOST_CHECK(r >= (rng.min)()); | |
27 | BOOST_CHECK(r <= (rng.max)()); | |
28 | } | |
29 | result_type val = rng(); | |
30 | BOOST_CHECK(val >= (rng.min)()); | |
31 | BOOST_CHECK(val <= (rng.max)()); | |
32 | // Get the result: | |
33 | bool result = val == expected; | |
34 | ||
35 | // allow for a simple eyeball check for MSVC instantiation brokenness | |
36 | // (if the numbers for all generators are the same, it's obviously broken) | |
37 | if(std::numeric_limits<R>::is_integer == 0) | |
38 | std::cout << std::setprecision(std::numeric_limits<R>::digits10 + 1); | |
39 | std::cout << val; | |
40 | if(result == 0) | |
41 | { | |
42 | std::cout << " (Expected: "; | |
43 | if(std::numeric_limits<R>::is_integer == 0) | |
44 | std::cout << std::setprecision(std::numeric_limits<R>::digits10 + 1); | |
45 | std::cout << expected << ")"; | |
46 | } | |
47 | std::cout << std::endl; | |
48 | BOOST_CHECK(result); | |
49 | } | |
50 | ||
51 | struct counting_functor | |
52 | { | |
53 | typedef unsigned result_type; | |
54 | unsigned operator()() | |
55 | { | |
56 | return i++; | |
57 | } | |
58 | counting_functor(int j) | |
59 | : i(j){} | |
60 | private: | |
61 | int i; | |
62 | }; | |
63 | ||
64 | int test_main(int, char*[]) | |
65 | { | |
66 | do{ | |
67 | typedef std::tr1::linear_congruential< ::boost::int32_t, 16807, 0, 2147483647> minstd_rand0; | |
68 | minstd_rand0 a1; | |
69 | minstd_rand0 a2(a1); | |
70 | validate("minstd_rand0", a1, 1043618065u); | |
71 | a1 = a2; | |
72 | validate("minstd_rand0", a2, 1043618065u); | |
73 | validate("minstd_rand0", a1, 1043618065u); | |
74 | a1.seed(1); | |
75 | counting_functor f1(1); | |
76 | a2.seed(f1); | |
77 | validate("minstd_rand0", a2, 1043618065u); | |
78 | validate("minstd_rand0", a1, 1043618065u); | |
79 | a1 = minstd_rand0(1); | |
80 | counting_functor f2(1); | |
81 | a2 = minstd_rand0(f2); | |
82 | validate("minstd_rand0", a2, 1043618065u); | |
83 | validate("minstd_rand0", a1, 1043618065u); | |
84 | }while(0); | |
85 | ||
86 | do{ | |
87 | typedef std::tr1::linear_congruential< ::boost::uint32_t, 69069, 0, 0> mt_seed; | |
88 | typedef std::tr1::mersenne_twister< ::boost::uint32_t,32,351,175,19,0xccab8ee7,11,7,0x31b6ab00,15,0xffe50000,17> mt11213b; | |
89 | mt11213b a1; | |
90 | mt11213b a2(a1); | |
91 | validate("mt11213b", a1, 3809585648u); | |
92 | a1 = a2; | |
93 | validate("mt11213b", a2, 3809585648u); | |
94 | validate("mt11213b", a1, 3809585648u); | |
95 | a1.seed(5489UL); | |
96 | validate("mt11213b", a1, 3809585648u); | |
97 | a1 = mt11213b(5489u); | |
98 | validate("mt11213b", a1, 3809585648u); | |
99 | mt_seed s1(4357UL); | |
100 | a1.seed(s1); | |
101 | validate("mt11213b", a1, 2742893714u); | |
102 | mt_seed s2(4357UL); | |
103 | mt11213b a3(s2); | |
104 | validate("mt11213b", a3, 2742893714u); | |
105 | }while(0); | |
106 | ||
107 | do{ | |
108 | typedef std::tr1::linear_congruential< :: boost::int32_t, 40014, 0, 2147483563> seed_t; | |
109 | typedef std::tr1::subtract_with_carry< ::boost::int32_t, (1<<24), 10, 24> sub_t; | |
110 | sub_t a1; | |
111 | sub_t a2(a1); | |
112 | validate("subtract_with_carry", a1, 7937952u); | |
113 | a1 = a2; | |
114 | validate("subtract_with_carry", a2, 7937952u); | |
115 | validate("subtract_with_carry", a1, 7937952u); | |
116 | a1.seed(0); | |
117 | seed_t s1(19780503UL); | |
118 | a2.seed(s1); | |
119 | validate("subtract_with_carry", a2, 7937952u); | |
120 | validate("subtract_with_carry", a1, 7937952u); | |
121 | a1 = sub_t(0); | |
122 | seed_t s2(19780503UL); | |
123 | a2 = sub_t(s2); | |
124 | validate("subtract_with_carry", a2, 7937952u); | |
125 | validate("subtract_with_carry", a1, 7937952u); | |
126 | }while(0); | |
127 | ||
128 | do{ | |
129 | typedef std::tr1::linear_congruential< :: boost::int32_t, 40014, 0, 2147483563> seed_t; | |
130 | typedef std::tr1::subtract_with_carry_01< float, 24, 10, 24> ranlux_base_01; | |
131 | ranlux_base_01 a1; | |
132 | ranlux_base_01 a2(a1); | |
133 | validate("ranlux_base_01", a1, 0.4731388F); | |
134 | a1 = a2; | |
135 | validate("ranlux_base_01", a2, 0.4731388F); | |
136 | validate("ranlux_base_01", a1, 0.4731388F); | |
137 | a1.seed(0); | |
138 | seed_t s1(19780503UL); | |
139 | a2.seed(s1); | |
140 | validate("ranlux_base_01", a2, 0.4731388F); | |
141 | validate("ranlux_base_01", a1, 0.4731388F); | |
142 | a1 = ranlux_base_01(0); | |
143 | seed_t s2(19780503UL); | |
144 | a2 = ranlux_base_01(s2); | |
145 | validate("ranlux_base_01", a2, 0.4731388F); | |
146 | validate("ranlux_base_01", a1, 0.4731388F); | |
147 | }while(0); | |
148 | ||
149 | do{ | |
150 | typedef std::tr1::linear_congruential< :: boost::int32_t, 40014, 0, 2147483563> seed_t; | |
151 | typedef std::tr1::subtract_with_carry_01< double, 48, 10, 24> ranlux64_base_01; | |
152 | ranlux64_base_01 a1; | |
153 | ranlux64_base_01 a2(a1); | |
154 | validate("ranlux64_base_01", a1, 0.1332451100961265); | |
155 | a1 = a2; | |
156 | validate("ranlux64_base_01", a2, 0.1332451100961265); | |
157 | validate("ranlux64_base_01", a1, 0.1332451100961265); | |
158 | a1.seed(0); | |
159 | seed_t s1(19780503UL); | |
160 | a2.seed(s1); | |
161 | validate("ranlux64_base_01", a2, 0.1332451100961265); | |
162 | validate("ranlux64_base_01", a1, 0.1332451100961265); | |
163 | a1 = ranlux64_base_01(0); | |
164 | seed_t s2(19780503UL); | |
165 | a2 = ranlux64_base_01(s2); | |
166 | validate("ranlux64_base_01", a2, 0.1332451100961265); | |
167 | validate("ranlux64_base_01", a1, 0.1332451100961265); | |
168 | }while(0); | |
169 | ||
170 | do{ | |
171 | typedef std::tr1::linear_congruential< :: boost::int32_t, 40014, 0, 2147483563> seed_t; | |
172 | typedef std::tr1::subtract_with_carry< ::boost::int32_t, (1<<24), 10, 24> sub_t; | |
173 | typedef std::tr1::xor_combine<sub_t, 0, sub_t, 3> xor_t; | |
174 | xor_t a1; | |
175 | validate("xor_combine", a1, 61989536u); | |
176 | a1 = xor_t(); | |
177 | xor_t a2(a1); | |
178 | validate("xor_combine", a1, 61989536u); | |
179 | a1 = a2; | |
180 | validate("xor_combine", a2, 61989536u); | |
181 | validate("xor_combine", a1, 61989536u); | |
182 | a1.seed(0); | |
183 | seed_t s1(19780503UL); | |
184 | a2.seed(s1); | |
185 | validate("xor_combine", a2, 90842400u); | |
186 | validate("xor_combine", a1, 114607192u); | |
187 | a1 = xor_t(0); | |
188 | seed_t s2(19780503UL); | |
189 | a2 = xor_t(s2); | |
190 | validate("xor_combine", a2, 90842400u); | |
191 | validate("xor_combine", a1, 114607192u); | |
192 | }while(0); | |
193 | ||
194 | do{ | |
195 | std::tr1::minstd_rand0 r1; | |
196 | validate("std::tr1::minstd_rand0", r1, 1043618065u); | |
197 | std::tr1::minstd_rand r2; | |
198 | validate("std::tr1::minstd_rand", r2, 399268537u); | |
199 | std::tr1::mt19937 r3; | |
200 | validate("std::tr1::mt19937", r3, 4123659995u); | |
201 | std::tr1::ranlux3 r4; | |
202 | validate("std::tr1::ranlux3", r4, 5957620u); | |
203 | std::tr1::ranlux4 r5; | |
204 | validate("std::tr1::ranlux4", r5, 8587295u); | |
205 | std::tr1::ranlux3_01 r6; | |
206 | validate("std::tr1::ranlux3_01", r6, 5957620.0F/std::pow(2.0f,24)); | |
207 | std::tr1::ranlux4_01 r7; | |
208 | validate("std::tr1::ranlux4_01", r7, 8587295.0F/std::pow(2.0f,24)); | |
209 | }while(0); | |
210 | ||
211 | return 0; | |
212 | } | |
213 |