Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d8cee2232a46f2e8c41961ebac86287911247cfe
[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
11 TEST_CASE("xbt::random: Random Number Generation")
12 {
13   SECTION("Using XBT_RNG_xbt")
14   {
15     simgrid::xbt::random::set_mersenne_seed(12345);
16
17     REQUIRE(simgrid::xbt::random::exponential(25) == 0.00291934351538427348);
18     REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == 4);
19     REQUIRE(simgrid::xbt::random::uniform_real(0, 1) == 0.31637556043369124970);
20     REQUIRE(simgrid::xbt::random::normal(0, 2) == 1.62746784745133976635);
21   }
22
23   SECTION("Using XBT_RNG_std")
24   {
25     std::mt19937 gen;
26     gen.seed(12345);
27
28     simgrid::xbt::random::set_mersenne_seed(12345);
29     simgrid::xbt::random::set_implem_std();
30
31     std::exponential_distribution<> distA(25);
32     std::uniform_int_distribution<> distB(1, 6);
33     std::uniform_real_distribution<> distC(0, 1);
34     std::normal_distribution<> distD(0, 2);
35
36     REQUIRE(simgrid::xbt::random::exponential(25) == distA(gen));
37     REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == distB(gen));
38     REQUIRE(simgrid::xbt::random::uniform_real(0, 1) == distC(gen));
39     REQUIRE(simgrid::xbt::random::normal(0, 2) == distD(gen));
40   }
41 }