Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 379baef

Browse files
authored
Merge pull request #285 from Bam4d/feat/cross_platform_rng_distribution
adapt rng generation to use boost for rng cross platform reproducibility
2 parents 29fb8e0 + 8ae744c commit 379baef

8 files changed

Lines changed: 129 additions & 17 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ find_package(yaml-cpp REQUIRED)
128128
# spdlog
129129
find_package(spdlog REQUIRED)
130130

131+
# boost
132+
find_package(Boost COMPONENTS Random REQUIRED)
133+
131134
if(NOT WASM)
132135

133136
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)

cmake/targets/griddly.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ target_link_libraries(
1919
yaml-cpp
2020
glm::glm
2121
spdlog::spdlog
22+
Boost::random
2223
)
2324

2425
# If we are not compiling WASM, add vulkan and stb
@@ -66,7 +67,7 @@ else()
6667
GRIDDLY_SOURCES
6768
EXCLUDE
6869
REGEX
69-
"src/Griddly/Core/Observers/Vulkan/.*"
70+
"${GRIDDLY_SRC_DIR}/Griddly/Core/Observers/Vulkan/.*"
7071
)
7172

7273
add_library(${GRIDDLY_LIB_NAME}_static STATIC ${GRIDDLY_SOURCES})

cmake/targets/wasm.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ set_target_properties(
1717
PROPERTIES
1818
LINK_FLAGS
1919
"-lembind -fexceptions -s ENVIRONMENT=web -s ALLOW_MEMORY_GROWTH=1 -sNO_DISABLE_EXCEPTION_THROWING -sASYNCIFY -sMODULARIZE=1"
20-
)
20+
)
21+

deps/conanfile.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,45 @@ yaml-cpp/0.6.3
77
spdlog/1.9.2
88
stb/20200203
99
volk/1.3.224.1
10+
boost/1.82.0
11+
1012

1113
[generators]
1214
CMakeToolchain
1315
CMakeDeps
1416
cmake_paths
17+
18+
[options]
19+
boost:shared=False
20+
boost:without_random=False
21+
boost:without_atomic=True
22+
boost:without_chrono=True
23+
boost:without_container=True
24+
boost:without_context=True
25+
boost:without_contract=True
26+
boost:without_coroutine=True
27+
boost:without_date_time=True
28+
boost:without_exception=True
29+
boost:without_fiber=True
30+
boost:without_filesystem=True
31+
boost:without_graph=True
32+
boost:without_graph_parallel=True
33+
boost:without_iostreams=True
34+
boost:without_json=True
35+
boost:without_locale=True
36+
boost:without_log=True
37+
boost:without_math=True
38+
boost:without_mpi=True
39+
boost:without_nowide=True
40+
boost:without_program_options=False
41+
boost:without_python=True
42+
boost:without_regex=True
43+
boost:without_serialization=True
44+
boost:without_stacktrace=True
45+
boost:without_system=False
46+
boost:without_test=True
47+
boost:without_thread=True
48+
boost:without_timer=True
49+
boost:without_type_erasure=True
50+
boost:without_url=True
51+
boost:without_wave=True

deps/wasm/conanfile_wasm.txt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,44 @@
22
glm/0.9.9.8
33
yaml-cpp/0.6.3
44
spdlog/1.9.2
5+
boost/1.82.0
56

67
[generators]
78
CMakeToolchain
89
CMakeDeps
910

10-
11+
[options]
12+
boost:shared=False
13+
boost:without_random=False
14+
boost:without_atomic=True
15+
boost:without_chrono=True
16+
boost:without_container=True
17+
boost:without_context=True
18+
boost:without_contract=True
19+
boost:without_coroutine=True
20+
boost:without_date_time=True
21+
boost:without_exception=True
22+
boost:without_fiber=True
23+
boost:without_filesystem=True
24+
boost:without_graph=True
25+
boost:without_graph_parallel=True
26+
boost:without_iostreams=True
27+
boost:without_json=True
28+
boost:without_locale=True
29+
boost:without_log=True
30+
boost:without_math=True
31+
boost:without_mpi=True
32+
boost:without_nowide=True
33+
boost:without_program_options=False
34+
boost:without_python=True
35+
boost:without_regex=True
36+
boost:without_serialization=True
37+
boost:without_stacktrace=True
38+
boost:without_system=False
39+
boost:without_test=True
40+
boost:without_thread=True
41+
boost:without_timer=True
42+
boost:without_type_erasure=True
43+
boost:without_url=True
44+
boost:without_wave=True
45+
boost:multithreading=False

src/Griddly/Core/Util/RandomGenerator.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
#include "RandomGenerator.hpp"
22

3+
#include "boost/random.hpp"
4+
5+
36
namespace griddly {
47

58

69
void RandomGenerator::seed(int32_t seed) {
710
randomGenerator_.seed(seed);
811
}
912

10-
const int32_t RandomGenerator::sampleInt(int32_t min, int32_t max) {
11-
std::uniform_int_distribution<int32_t> dist(min, max);
13+
int32_t RandomGenerator::sampleInt(int32_t min, int32_t max) {
14+
boost::uniform_int<int32_t> dist(min, max);
1215
return dist(randomGenerator_);
1316
}
1417

15-
const float RandomGenerator::sampleFloat(float min, float max) {
16-
std::uniform_real_distribution<float> dist(min, max);
18+
float RandomGenerator::sampleFloat(float min, float max) {
19+
boost::uniform_real<float> dist(min, max);
1720
return dist(randomGenerator_);
1821
}
1922

src/Griddly/Core/Util/RandomGenerator.hpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22

33
namespace griddly {
44

5-
class RandomGenerator {
6-
public:
7-
virtual void seed(int32_t seed);
5+
class RandomGenerator {
6+
public:
7+
RandomGenerator(size_t seed = 0) : randomGenerator_(seed) {}
88

9-
virtual const int32_t sampleInt(int32_t min, int32_t max);
9+
virtual ~RandomGenerator() = default;
1010

11-
virtual const float sampleFloat(float min, float max);
11+
virtual void seed(int32_t seed);
1212

13-
virtual std::mt19937& getEngine();
13+
virtual int32_t sampleInt(int32_t min, int32_t max);
1414

15-
private:
16-
// Random number generator for the grid and associated objects
17-
std::mt19937 randomGenerator_ = std::mt19937();
18-
};
15+
virtual float sampleFloat(float min, float max);
16+
17+
virtual std::mt19937 &getEngine();
18+
19+
private:
20+
// Random number generator for the grid and associated objects
21+
std::mt19937 randomGenerator_;
22+
};
1923

2024
} // namespace griddly

tests/src/Griddly/Core/RngTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "gtest/gtest.h"
2+
#include "Griddly/Core/Util/RandomGenerator.hpp"
3+
4+
const int32_t EXPECTED_INT_SAMPLE = 5334164;
5+
const float EXPECTED_FLOAT_SAMPLE = 432876.375;
6+
7+
8+
TEST(RandomNumberGeneration, uniform_int_reproducibility) {
9+
auto rng = griddly::RandomGenerator(0);
10+
const int32_t upper_limit = 10'000'000;
11+
const size_t advance_by_n = 100'100;
12+
for (size_t i = 0; i < advance_by_n; i++) {
13+
rng.sampleInt(0, upper_limit);
14+
}
15+
auto sample = rng.sampleInt(0, upper_limit);
16+
ASSERT_EQ(sample, EXPECTED_INT_SAMPLE);
17+
}
18+
19+
TEST(RandomNumberGeneration, uniform_real_reproducibility) {
20+
auto rng = griddly::RandomGenerator(0);
21+
const float upper_limit = 1'000'000.;
22+
const size_t advance_by_n = 100'100;
23+
for (size_t i = 0; i < advance_by_n; i++) {
24+
rng.sampleFloat(0, upper_limit);
25+
}
26+
auto sample = rng.sampleFloat(0, upper_limit);
27+
ASSERT_EQ(sample, EXPECTED_FLOAT_SAMPLE);
28+
}

0 commit comments

Comments
 (0)