Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c964cb861bf13e9439604fdddb08de841b490000
[simgrid.git] / src / xbt / random_test.cpp
1 /* Copyright (c) 2019. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/include/catch.hpp"
7 #include "xbt/log.h"
8 #include "xbt/random.hpp"
9 #include <random>
10 #include <cmath>
11
12 TEST_CASE("xbt::random: Random Number Generation")
13 {
14   SECTION("Using XBT_RNG_xbt")
15   {
16     simgrid::xbt::random::set_mersenne_seed(12345);
17
18     REQUIRE(fabs(simgrid::xbt::random::exponential(25) - 0.00291934351538427348) <= 1e-14);
19     REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == 4);
20     REQUIRE(fabs(simgrid::xbt::random::uniform_real(0, 1) - 0.31637556043369124970) <= 1e-14);
21     REQUIRE(fabs(simgrid::xbt::random::normal(0, 2) - 1.62746784745133976635) <= 1e-14);
22   }
23
24   SECTION("Using XBT_RNG_std")
25   {
26     std::mt19937 gen;
27     gen.seed(12345);
28
29     simgrid::xbt::random::set_mersenne_seed(12345);
30     simgrid::xbt::random::set_implem_std();
31
32     std::exponential_distribution<> distA(25);
33     std::uniform_int_distribution<> distB(1, 6);
34     std::uniform_real_distribution<> distC(0, 1);
35     std::normal_distribution<> distD(0, 2);
36
37     REQUIRE(fabs(simgrid::xbt::random::exponential(25) - distA(gen)) <= 1e-14);
38     REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == distB(gen));
39     REQUIRE(fabs(simgrid::xbt::random::uniform_real(0, 1) - distC(gen)) <= 1e-14);
40     REQUIRE(fabs(simgrid::xbt::random::normal(0, 2) - distD(gen)) <= 1e-14);
41   }
42 }