1 /* Copyright (c) 2019. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/include/catch.hpp"
8 #include "xbt/random.hpp"
12 #define EPSILON (100*std::numeric_limits<double>::epsilon())
14 TEST_CASE("xbt::random: Random Number Generation")
16 SECTION("Using XBT_RNG_xbt")
18 simgrid::xbt::random::set_mersenne_seed(12345);
19 REQUIRE(simgrid::xbt::random::exponential(25) == Approx(0.00291934351538427348).epsilon(EPSILON));
20 REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == 4);
21 REQUIRE(simgrid::xbt::random::uniform_real(0, 1) == Approx(0.31637556043369124970).epsilon(EPSILON));
22 REQUIRE(simgrid::xbt::random::normal(0, 2) == Approx(1.62746784745133976635).epsilon(EPSILON));
25 SECTION("Using XBT_RNG_std")
30 simgrid::xbt::random::set_mersenne_seed(12345);
31 simgrid::xbt::random::set_implem_std();
33 std::exponential_distribution<> distA(25);
34 std::uniform_int_distribution<> distB(1, 6);
35 std::uniform_real_distribution<> distC(0, 1);
36 std::normal_distribution<> distD(0, 2);
38 REQUIRE(simgrid::xbt::random::exponential(25) == Approx(distA(gen)).epsilon(EPSILON));
39 REQUIRE(simgrid::xbt::random::uniform_int(1, 6) == distB(gen));
40 REQUIRE(simgrid::xbt::random::uniform_real(0, 1) == Approx(distC(gen)).epsilon(EPSILON));
41 REQUIRE(simgrid::xbt::random::normal(0, 2) == Approx(distD(gen)).epsilon(EPSILON));