1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/****************************************************************************
*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2023 by Inria. All rights reserved.
*
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
*
* See https://visp.inria.fr for more information.
*
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
*
* If you have questions regarding the use of this file, please contact
* Inria at visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Test pseudo random number generator.
*
*****************************************************************************/
#include <visp3/core/vpConfig.h>
#ifdef VISP_HAVE_CATCH2
#define CATCH_CONFIG_RUNNER
#include <catch.hpp>
#include <visp3/core/vpGaussRand.h>
#include <visp3/core/vpMath.h>
#include <visp3/core/vpTime.h>
namespace
{
class vpUniRandOld
{
long a;
long m; // 2^31-1
long q; // integer part of m/a
long r; // r=m mod a
double normalizer; // we use a normalizer > m to ensure ans will never be 1
// (it is the case if x = 739806647)
private:
inline void draw0()
{
long k = x / q; // temp value for computing without overflow
x = a * (x - k * q) - k * r;
if (x < 0)
x += m; // compute x without overflow
}
protected:
long x;
double draw1()
{
const long ntab = 33; // we work on a 32 elements array.
// the 33rd one is actually the first value of y.
const long modulo = ntab - 2;
static long y = 0;
static long T[ntab];
long j; // index of T
// step 0
if (!y) { // first time
for (j = 0; j < ntab; j++) {
draw0();
T[j] = x;
} // compute table T
y = T[ntab - 1];
}
// step 1
j = y & modulo; // compute modulo ntab+1 (the first element is the 0th)
// step 3
y = T[j];
double ans = (double)y / normalizer;
// step 4
// generate x(k+i) and set y=x(k+i)
draw0();
// refresh T[j];
T[j] = x;
return ans;
}
public:
//! Default constructor.
explicit vpUniRandOld(const long seed = 0)
: a(16807), m(2147483647), q(127773), r(2836), normalizer(2147484721.0), x((seed) ? seed : 739806647)
{
}
//! Default destructor.
virtual ~vpUniRandOld(){};
//! Operator that allows to get a random value.
double operator()() { return draw1(); }
};
} // namespace
TEST_CASE("Check Gaussian draw", "[visp_rand]")
{
std::vector<double> vec(100000);
const double sigma = 5.0, mean = -7.5;
vpGaussRand rng(sigma, mean);
vpChrono chrono;
chrono.start();
for (size_t i = 0; i < vec.size(); i++) {
vec[i] = rng();
}
chrono.stop();
std::cout << vec.size() << " Gaussian draw performed in " << chrono.getDurationMs() << " ms" << std::endl;
double calculated_sigma = vpMath::getStdev(vec);
double calculated_mean = vpMath::getMean(vec);
std::cout << "Calculated sigma: " << calculated_sigma << std::endl;
std::cout << "Calculated mean: " << calculated_mean << std::endl;
CHECK(calculated_sigma == Approx(sigma).epsilon(0.01));
CHECK(calculated_mean == Approx(mean).epsilon(0.01));
}
TEST_CASE("Check Gaussian draw independance", "[visp_rand]")
{
const double sigma = 5.0, mean = -7.5;
SECTION("Two simultaneous vpGaussRand instances with the same seed should produce the same results")
{
vpGaussRand rng1(sigma, mean), rng2(sigma, mean);
for (int i = 0; i < 10; i++) {
CHECK(rng1() == Approx(rng2()).margin(1e-6));
}
}
SECTION("Two vpGaussRand instances with the same seed should produce the same results")
{
std::vector<double> vec1, vec2;
{
vpGaussRand rng(sigma, mean);
for (int i = 0; i < 10; i++) {
vec1.push_back(rng());
}
}
{
vpGaussRand rng(sigma, mean);
for (int i = 0; i < 10; i++) {
vec2.push_back(rng());
}
}
REQUIRE(vec1.size() == vec2.size());
for (size_t i = 0; i < vec1.size(); i++) {
CHECK(vec1[i] == Approx(vec2[i]).margin(1e-6));
}
}
}
TEST_CASE("Check uniform draw", "[visp_rand]")
{
const int niters = 500000;
SECTION("vpUniRand")
{
vpUniRand rng;
int inside = 0;
vpChrono chrono;
chrono.start();
for (int i = 0; i < niters; i++) {
double x = rng();
double y = rng();
if (sqrt(x * x + y * y) <= 1.0) {
inside++;
}
}
double pi = 4.0 * inside / niters;
chrono.stop();
double pi_error = pi - M_PI;
std::cout << "vpUniRand calculated pi: " << pi << " in " << chrono.getDurationMs() << " ms" << std::endl;
std::cout << "pi error: " << pi_error << std::endl;
CHECK(pi == Approx(M_PI).margin(0.005));
}
SECTION("C++ rand()")
{
srand(0);
int inside = 0;
vpChrono chrono;
chrono.start();
for (int i = 0; i < niters; i++) {
double x = static_cast<double>(rand()) / RAND_MAX;
double y = static_cast<double>(rand()) / RAND_MAX;
if (sqrt(x * x + y * y) <= 1.0) {
inside++;
}
}
double pi = 4.0 * inside / niters;
chrono.stop();
double pi_error = pi - M_PI;
std::cout << "C++ rand() calculated pi: " << pi << " in " << chrono.getDurationMs() << " ms" << std::endl;
std::cout << "pi error: " << pi_error << std::endl;
CHECK(pi == Approx(M_PI).margin(0.01));
}
SECTION("Old ViSP vpUniRand implementation")
{
vpUniRand rng;
int inside = 0;
vpChrono chrono;
chrono.start();
for (int i = 0; i < niters; i++) {
double x = rng();
double y = rng();
if (sqrt(x * x + y * y) <= 1.0) {
inside++;
}
}
double pi = 4.0 * inside / niters;
chrono.stop();
double pi_error = pi - M_PI;
std::cout << "Old ViSP vpUniRand implementation calculated pi: " << pi << " in " << chrono.getDurationMs() << " ms"
<< std::endl;
std::cout << "pi error: " << pi_error << std::endl;
CHECK(pi == Approx(M_PI).margin(0.005));
}
}
TEST_CASE("Check uniform draw range", "[visp_rand]")
{
const int niters = 1000;
vpUniRand rng;
SECTION("Check[0.0, 1.0) range")
{
for (int i = 0; i < niters; i++) {
double r = rng();
CHECK(r >= 0.0);
CHECK(r < 1.0);
}
}
SECTION("Check[-7, 10) range")
{
const int a = -7, b = 10;
for (int i = 0; i < niters; i++) {
int r = rng.uniform(a, b);
CHECK(r >= a);
CHECK(r < b);
}
}
SECTION("Check[-4.5f, 105.3f) range")
{
const float a = -4.5f, b = 105.3f;
for (int i = 0; i < niters; i++) {
float r = rng.uniform(a, b);
CHECK(r >= a);
CHECK(r < b);
}
}
SECTION("Check[14.6, 56.78) range")
{
const double a = 14.6, b = 56.78;
for (int i = 0; i < niters; i++) {
double r = rng.uniform(a, b);
CHECK(r >= a);
CHECK(r < b);
}
}
}
TEST_CASE("Check uniform draw independance", "[visp_rand]")
{
SECTION("Two simultaneous vpUniRand instances with the same seed should produce the same results")
{
{
vpUniRand rng1, rng2;
for (int i = 0; i < 10; i++) {
CHECK(rng1.next() == rng2.next());
}
}
{
vpUniRand rng1, rng2;
for (int i = 0; i < 10; i++) {
CHECK(rng1.uniform(-1.0, 1.0) == Approx(rng2.uniform(-1.0, 1.0)).margin(1e-6));
}
}
}
SECTION("Two vpUniRand instances with the same seed should produce the same results")
{
{
std::vector<uint32_t> vec1, vec2;
{
vpUniRand rng;
for (int i = 0; i < 10; i++) {
vec1.push_back(rng.next());
}
}
{
vpUniRand rng;
for (int i = 0; i < 10; i++) {
vec2.push_back(rng.next());
}
}
REQUIRE(vec1.size() == vec2.size());
for (size_t i = 0; i < vec1.size(); i++) {
CHECK(vec1[i] == vec2[i]);
}
}
{
std::vector<double> vec1, vec2;
{
vpUniRand rng;
for (int i = 0; i < 10; i++) {
vec1.push_back(rng.uniform(-1.0, 2.0));
}
}
{
vpUniRand rng;
for (int i = 0; i < 10; i++) {
vec2.push_back(rng.uniform(-1.0, 2.0));
}
}
REQUIRE(vec1.size() == vec2.size());
for (size_t i = 0; i < vec1.size(); i++) {
CHECK(vec1[i] == Approx(vec2[i]).margin(1e-6));
}
}
}
}
int main(int argc, char *argv[])
{
Catch::Session session; // There must be exactly one instance
// Let Catch (using Clara) parse the command line
session.applyCommandLine(argc, argv);
int numFailed = session.run();
// numFailed is clamped to 255 as some unices only use the lower 8 bits.
// This clamping has already been applied, so just return it here
// You can also do any post run clean-up here
return numFailed;
}
#else
int main() { return EXIT_SUCCESS; }
#endif
|