Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A first version of the xbt/random module
authorYann Duplouy <yann.duplouy@inria.fr>
Mon, 21 Oct 2019 09:15:02 +0000 (11:15 +0200)
committerYann Duplouy <yann.duplouy@inria.fr>
Mon, 21 Oct 2019 09:15:02 +0000 (11:15 +0200)
include/xbt/random.hpp [new file with mode: 0644]
src/xbt/random.cpp [new file with mode: 0644]
tools/cmake/DefinePackages.cmake

diff --git a/include/xbt/random.hpp b/include/xbt/random.hpp
new file mode 100644 (file)
index 0000000..e60f387
--- /dev/null
@@ -0,0 +1,20 @@
+/* Copyright (c) 2019. The SimGrid Team. All rights reserved.               */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#ifndef SIMGRID_XBT_RANDOM_HPP
+#define SIMGRID_XBT_RANDOM_HPP
+
+#include <random>
+
+namespace simgrid {
+namespace xbt {
+namespace random {
+int uniform_int(int min, int max);
+double uniform_real(double min, double max);
+} // namespace random
+} // namespace xbt
+} // namespace simgrid
+
+#endif
diff --git a/src/xbt/random.cpp b/src/xbt/random.cpp
new file mode 100644 (file)
index 0000000..95d4922
--- /dev/null
@@ -0,0 +1,44 @@
+/* Copyright (c) 2019. The SimGrid Team. All rights reserved.               */
+
+/* This program is free software; you can redistribute it and/or modify it
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "xbt/random.hpp"
+#include "xbt/asserts.h"
+#include <random>
+
+namespace simgrid {
+namespace xbt {
+namespace random {
+std::mt19937 mt19937_gen;
+
+int uniform_int(int min, int max)
+{
+  unsigned long gmin   = mt19937_gen.min();
+  unsigned long gmax   = mt19937_gen.max();
+  unsigned long grange = gmax - gmin + 1;
+  unsigned long range  = max - min + 1;
+  xbt_assert(range < grange || range == grange, "The current implementation of the uniform integer distribution does "
+                                                "not allow range to be higher than mt19937's range");
+  unsigned long mult       = grange / range;
+  unsigned long maxallowed = gmin + (mult + 1) * range - 1;
+  while (true) {
+    unsigned long value = mt19937_gen();
+    if (value > maxallowed) {
+    } else {
+      return value % range + min;
+    }
+  }
+}
+
+double uniform_real(double min, double max)
+{
+  // This reuses Boost's uniform real distribution ideas
+  unsigned long numerator = mt19937_gen() - mt19937_gen.min();
+  unsigned long divisor   = mt19937_gen.max() - mt19937_gen.min();
+  return min + (max - min) * numerator / divisor;
+}
+
+} // namespace random
+} // namespace xbt
+} // namespace simgrid
index 180f99e..385a7e4 100644 (file)
@@ -282,6 +282,7 @@ set(XBT_SRC
   src/xbt/memory_map.hpp
   src/xbt/OsSemaphore.hpp
   src/xbt/parmap.cpp
+  src/xbt/random.cpp
   src/xbt/snprintf.c
   src/xbt/string.cpp
   src/xbt/xbt_log_appender_file.cpp
@@ -772,6 +773,7 @@ set(headers_to_install
   include/xbt/module.h
   include/xbt/parmap.h
   include/xbt/range.hpp
+  include/xbt/random.hpp
   include/xbt/replay.hpp
   include/xbt/signal.hpp
   include/xbt/str.h