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

Skip to content

Commit 309bdad

Browse files
committed
hotfixing for random number generator being totally broken
1 parent 1fe8c37 commit 309bdad

7 files changed

Lines changed: 59 additions & 24 deletions

File tree

python/examples/random_movement/random_spiders.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Objects:
9090
Isometric:
9191
- Image: oryx/oryx_iso_dungeon/water-1.png
9292
Offset: [0, 4]
93+
TilingMode: ISO_FLOOR
9394
Block2D:
9495
- Color: [ 0.0, 0.0, 0.8 ]
9596
Shape: square

src/Griddly/Core/GDY/Objects/Object.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,9 @@ SingleInputMapping Object::getInputMapping(std::string actionName, uint32_t acti
601601
if (actionInputsDefinition.mapToGrid) {
602602
spdlog::debug("Getting mapped to grid mapping for action {0}", actionName);
603603

604-
std::uniform_int_distribution<uint32_t> grid_location_width_distribution(0, grid()->getWidth() - 1);
605-
std::uniform_int_distribution<uint32_t> grid_location_height_distribution(0, grid()->getHeight() - 1);
606-
auto rand_x = grid_location_width_distribution(randomGenerator);
607-
auto rand_y = grid_location_height_distribution(randomGenerator);
604+
605+
auto rand_x = randomGenerator->sampleInt(0, grid()->getWidth() - 1);
606+
auto rand_y = randomGenerator->sampleInt(0, grid()->getHeight() - 1);
608607

609608
resolvedInputMapping.destinationLocation = {rand_x, rand_y};
610609

@@ -613,8 +612,8 @@ SingleInputMapping Object::getInputMapping(std::string actionName, uint32_t acti
613612
InputMapping inputMapping;
614613
if (randomize) {
615614
auto it = inputMappings.begin();
616-
std::uniform_int_distribution<uint32_t> inputMappingsDistribution(0, inputMappings.size() - 1);
617-
std::advance(it, inputMappingsDistribution(randomGenerator));
615+
auto sampledIdx = randomGenerator->sampleInt(0, inputMappings.size() - 1);
616+
std::advance(it, sampledIdx);
618617
inputMapping = it->second;
619618
} else if (actionId > 0) {
620619
auto it = inputMappings.find(actionId);

src/Griddly/Core/Grid.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ std::unordered_map<uint32_t, int32_t> Grid::executeAction(uint32_t playerId, std
203203
spdlog::debug("Executing action {0} with probability {1}", action->getDescription(), executionProbability);
204204

205205
if (executionProbability < 1.0) {
206-
std::uniform_real_distribution<float> actionExecutionDistribution;
207-
auto actionProbability = actionExecutionDistribution(randomGenerator_);
206+
auto actionProbability = randomGenerator_->sampleFloat(0,1);
208207
if (actionProbability > executionProbability) {
209208
spdlog::debug("Action aborted due to probability check {0} > {1}", actionProbability, executionProbability);
210209
return {};
@@ -620,10 +619,10 @@ void Grid::addObject(glm::ivec2 location, std::shared_ptr<Object> object, bool a
620619
}
621620

622621
void Grid::seedRandomGenerator(uint32_t seed) {
623-
randomGenerator_.seed(seed);
622+
randomGenerator_->seed(seed);
624623
}
625624

626-
const std::mt19937& Grid::getRandomGenerator() const {
625+
std::shared_ptr<RandomGenerator> Grid::getRandomGenerator() const {
627626
return randomGenerator_;
628627
}
629628

src/Griddly/Core/Grid.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "GDY/Objects/Object.hpp"
1515
#include "LevelGenerators/LevelGenerator.hpp"
1616
#include "Util/util.hpp"
17+
#include "Util/RandomGenerator.hpp"
1718

1819
#define GLM_ENABLE_EXPERIMENTAL
1920
#include <glm/glm.hpp>
@@ -169,7 +170,7 @@ class Grid : public std::enable_shared_from_this<Grid> {
169170

170171
virtual void seedRandomGenerator(uint32_t seed);
171172

172-
virtual const std::mt19937& getRandomGenerator() const;
173+
virtual std::shared_ptr<RandomGenerator> getRandomGenerator() const;
173174

174175
private:
175176
GridEvent buildGridEvent(std::shared_ptr<Action> action, uint32_t playerId, uint32_t tick);
@@ -228,8 +229,8 @@ class Grid : public std::enable_shared_from_this<Grid> {
228229
// Allows a subset of actions like "spawn" to be performed in empty space.
229230
std::unordered_map<uint32_t, std::shared_ptr<Object>> defaultObject_;
230231

231-
// Random number generator for the grid and associated objects
232-
std::mt19937 randomGenerator_ = std::mt19937();
232+
std::shared_ptr<RandomGenerator> randomGenerator_ = std::make_shared<RandomGenerator>(RandomGenerator());
233+
233234
};
234235

235236
} // namespace griddly
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "RandomGenerator.hpp"
2+
3+
namespace griddly {
4+
5+
6+
void RandomGenerator::seed(int32_t seed) {
7+
randomGenerator_.seed(seed);
8+
}
9+
10+
const int32_t RandomGenerator::sampleInt(int32_t min, int32_t max) {
11+
std::uniform_int_distribution<int32_t> dist(min, max);
12+
return dist(randomGenerator_);
13+
}
14+
15+
const float RandomGenerator::sampleFloat(float min, float max) {
16+
std::uniform_real_distribution<float> dist(min, max);
17+
return dist(randomGenerator_);
18+
}
19+
20+
} // namespace griddly
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <random>
2+
3+
namespace griddly {
4+
5+
class RandomGenerator {
6+
public:
7+
virtual void seed(int32_t seed);
8+
9+
virtual const int32_t sampleInt(int32_t min, int32_t max);
10+
11+
virtual const float sampleFloat(float min, float max);
12+
13+
private:
14+
// Random number generator for the grid and associated objects
15+
std::mt19937 randomGenerator_ = std::mt19937();
16+
};
17+
18+
} // namespace griddly

tests/src/Griddly/Core/GridTest.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -954,14 +954,11 @@ TEST(GridTest, randomNumberGenerator) {
954954
auto randomGenerator1 = grid1->getRandomGenerator();
955955
auto randomGenerator2 = grid2->getRandomGenerator();
956956

957-
std::uniform_int_distribution<uint32_t> distribution1(0, 10);
958-
std::uniform_int_distribution<uint32_t> distribution2(10, 20);
957+
auto randomResult111 = randomGenerator1->sampleInt(0, 10);
958+
auto randomResult121 = randomGenerator1->sampleInt(10, 20);
959959

960-
auto randomResult111 = distribution1(randomGenerator1);
961-
auto randomResult121 = distribution2(randomGenerator1);
962-
963-
auto randomResult211 = distribution1(randomGenerator2);
964-
auto randomResult221 = distribution2(randomGenerator2);
960+
auto randomResult211 = randomGenerator2->sampleInt(0, 10);
961+
auto randomResult221 = randomGenerator2->sampleInt(10, 20);
965962

966963
ASSERT_EQ(randomResult111, randomResult211);
967964
ASSERT_EQ(randomResult121, randomResult221);
@@ -972,11 +969,11 @@ TEST(GridTest, randomNumberGenerator) {
972969
randomGenerator1 = grid1->getRandomGenerator();
973970
randomGenerator2 = grid2->getRandomGenerator();
974971

975-
auto randomResult112 = distribution1(randomGenerator1);
976-
auto randomResult122 = distribution2(randomGenerator1);
972+
auto randomResult112 = randomGenerator1->sampleInt(0, 10);
973+
auto randomResult122 = randomGenerator1->sampleInt(10, 20);
977974

978-
auto randomResult212 = distribution1(randomGenerator2);
979-
auto randomResult222 = distribution2(randomGenerator2);
975+
auto randomResult212 = randomGenerator2->sampleInt(0, 10);
976+
auto randomResult222 = randomGenerator2->sampleInt(10, 20);
980977

981978
ASSERT_EQ(randomResult112, randomResult212);
982979
ASSERT_EQ(randomResult122, randomResult222);

0 commit comments

Comments
 (0)