Debian Codegraph @

src/ImathTest/testRandom.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 #ifdef NDEBUG
7 # undef NDEBUG
8 #endif
9
10 #include <ImathFun.h>
11 #include <ImathRandom.h>
12 #include <ImathVec.h>
13 #include <assert.h>
14 #include <iomanip>
15 #include <iostream>
16 #include "testRandom.h"
17
18 // Include ImathForward *after* other headers to validate forward declarations
19 #include <ImathForward.h>
20
21 using namespace std;
22 using IMATH_INTERNAL_NAMESPACE::abs;
23
24 namespace
25 {
26
27 void
28 testErand48()
29 {
30 //
31 // Our implementation of erand48(), nrand48(), etc.
32 // assumes that sizeof (unsigned short) == 2.
33 //
34
35 assert (sizeof (unsigned short) == 2);
36
37 //
38 // starting with a given seed, erand48() and nrand48()
39 // must generate the same sequence as the standard
40 // Unix/Linux functions.
41 //
42
43 unsigned short state[3];
44 state[0] = 0;
45 state[1] = 1;
46 state[2] = 2;
47
48 assert (abs (IMATH_INTERNAL_NAMESPACE::erand48 (state) - 0.671004) < 0.00001);
49 assert (abs (IMATH_INTERNAL_NAMESPACE::erand48 (state) - 0.786905) < 0.00001);
50 assert (abs (IMATH_INTERNAL_NAMESPACE::erand48 (state) - 0.316850) < 0.00001);
51 assert (abs (IMATH_INTERNAL_NAMESPACE::erand48 (state) - 0.384870) < 0.00001);
52 assert (abs (IMATH_INTERNAL_NAMESPACE::erand48 (state) - 0.854650) < 0.00001);
53
54 assert (IMATH_INTERNAL_NAMESPACE::nrand48 (state) == 0x4f4e8cb0);
55 assert (IMATH_INTERNAL_NAMESPACE::nrand48 (state) == 0x063e864b);
56 assert (IMATH_INTERNAL_NAMESPACE::nrand48 (state) == 0x2d10f1dd);
57 assert (IMATH_INTERNAL_NAMESPACE::nrand48 (state) == 0x1aadc122);
58 assert (IMATH_INTERNAL_NAMESPACE::nrand48 (state) == 0x1836a71f);
59
60 assert (state[0] == 0x2a42);
61 assert (state[1] == 0x4e3e);
62 assert (state[2] == 0x306d);
63 }
64
65 void
66 testDrand48 ()
67 {
68 std::cout << "testDrand48()" << std::endl;
69
70 //
71 // Confirm that drand48/lrand48 return the same values as
72 // erand48/nrand48 with the same seed.
73 //
74
75 unsigned short state[3];
76 state[2] = 0;
77 state[1] = 0;
78 state[0] = 0x330e;
79
80 IMATH_INTERNAL_NAMESPACE::srand48(0);
81
82 double e = IMATH_INTERNAL_NAMESPACE::erand48(state);
83 double d = IMATH_INTERNAL_NAMESPACE::drand48();
84 assert (e == d);
85
86 state[2] = 0;
87 state[1] = 0;
88 state[0] = 0x330e;
89
90 IMATH_INTERNAL_NAMESPACE::srand48(0);
91
92 long int n = IMATH_INTERNAL_NAMESPACE::nrand48(state);
93 long int l = IMATH_INTERNAL_NAMESPACE::lrand48();
94 assert (l == n);
95 }
96
97 template <class Rand>
98 void
99 testGenerator()
100 {
101 //
102 // Test if the values, and the differences between
103 // successive values, are evenly distributed.
104 //
105
106 const int N = 10;
107 const int M = 100000;
108
109 int values[N + 1];
110 int diffs[2 * N + 3];
111 int* v = &values[0];
112 int* d = &diffs[N + 2];
113
114 for (int i = 0; i <= N; ++i)
115 v[i] = 0;
116
117 for (int i = -N; i <= N; ++i)
118 d[i] = 0;
119
120 Rand rand (0);
121 float previous = 0;
122
123 for (int i = 0; i < M * N; ++i)
124 {
125 float r = float(rand.nextf (0.0, 1.0));
126 float diff = r - previous;
127 previous = r;
128
129 v[int (r * N)] += 1;
130 d[IMATH_INTERNAL_NAMESPACE::floor (diff * N + 0.5)] += 1;
131 }
132
133 cout << " values" << endl;
134
135 for (int i = 0; i < N; ++i)
136 {
137 // cout << setw (4) << i << ' ' << setw(6) << v[i] << ' ';
138 assert (abs (v[i] - M) < 0.01 * M);
139
140 // for (int j = 0; j < v[i] * 60 / M; ++j)
141 // cout << '*';
142
143 // cout << endl;
144 }
145
146 assert (v[N] == 0);
147
148 cout << " differences between successive values" << endl;
149
150 for (int i = -N; i <= N; ++i)
151 {
152 // cout << setw (4) << i << ' ' << setw (6) << d[i] << ' ';
153 assert (abs ((N - abs (i)) * M / N - d[i]) < 0.05 * M);
154
155 // for (int j = 0; j < d[i] * 60 / M; ++j)
156 // cout << '*';
157
158 // cout << endl;
159 }
160
161 cout << " range" << endl;
162
163 double rMin = 1.0;
164 double rMax = 0.0;
165
166 for (int i = 0; i <= 10000000; ++i)
167 {
168 double r = rand.nextf (0.0, 1.0);
169
170 if (rMin > r)
171 rMin = r;
172
173 if (rMax < r)
174 rMax = r;
175 }
176
177 assert (rMin < 0.0001 && rMax > 0.9999);
178
179 const double pow_2_60 = double (1073741824) * double (1073741824);
180
181 for (int i = 0; i <= 10000000; ++i)
182 {
183 double r0 = rand.nextf (-2.0, 3.0);
184 assert (r0 >= -2.0 && r0 <= 3.0);
185
186 double r1 = rand.nextf (-pow_2_60, 1);
187 assert (r1 >= -pow_2_60 && r1 <= 1);
188
189 double r2 = rand.nextf (-1, pow_2_60);
190 assert (r2 >= -1 && r2 <= pow_2_60);
191 }
192 }
193
194 template <class Rand>
195 void
196 testSolidSphere()
197 {
198 const int N = 10;
199 const int M = 10000;
200 int v[N + 1];
201
202 for (int i = 0; i <= N; ++i)
203 v[i] = 0;
204
205 Rand rand (0);
206
207 for (int i = 0; i < M * N; ++i)
208 {
209 IMATH_INTERNAL_NAMESPACE::V3f p =
210 IMATH_INTERNAL_NAMESPACE::solidSphereRand<IMATH_INTERNAL_NAMESPACE::V3f> (rand);
211 float l = p.length();
212 v[IMATH_INTERNAL_NAMESPACE::floor (l * N)] += 1;
213
214 assert (l < 1.00001);
215 }
216
217 for (int i = 0; i < N; ++i)
218 assert (v[i] > 0);
219 }
220
221 template <class Rand>
222 void
223 testHollowSphere()
224 {
225 const int M = 100000;
226 Rand rand (0);
227
228 for (int i = 0; i < M; ++i)
229 {
230 IMATH_INTERNAL_NAMESPACE::V3f p =
231 IMATH_INTERNAL_NAMESPACE::hollowSphereRand<IMATH_INTERNAL_NAMESPACE::V3f> (rand);
232 float l = p.length();
233
234 assert (abs (l - 1) < 0.00001);
235 }
236 }
237
238 } // namespace
239
240 void
241 testRandom()
242 {
243 cout << "Testing random number generators" << endl;
244
245 cout << "erand48(), nrand48()" << endl;
246 testErand48();
247
248 cout << "drand48(), lrand48()" << endl;
249 testDrand48 ();
250
251 cout << "Rand32" << endl;
252 testGenerator<IMATH_INTERNAL_NAMESPACE::Rand32>();
253
254 cout << "Rand48" << endl;
255 testGenerator<IMATH_INTERNAL_NAMESPACE::Rand48>();
256
257 cout << "Rand48" << endl;
258 testGenerator<IMATH_INTERNAL_NAMESPACE::Rand48> ();
259
260
261 cout << "solidSphereRand()" << endl;
262 testSolidSphere<IMATH_INTERNAL_NAMESPACE::Rand32>();
263
264 cout << "hollowSphereRand()" << endl;
265 testHollowSphere<IMATH_INTERNAL_NAMESPACE::Rand32>();
266
267 cout << "ok\n" << endl;
268 }