I'm working on a snake ai. For mutating the neural network, or deciding where apple shall be spawned, I make use of "random" header. There is how I generate numbers:
std::random_device randomDevice;
std::mt19937 mt(randomDevice());
int random(int min, int max) {
return std::uniform_int_distribution<int>(min, max)(mt);
}
float random(float min, float max) {
return std::uniform_real_distribution<float>(min, max)(mt);
}
// Example
T2<int> applePos {random(0, mapSize), random(0, mapSize)};
T2<float> neuralLink {random(-2.f, 2.f), random(-2.f, 2.f)};
The initial results are pretty consistent, distributed, and good to work with. I have a graph where I plot how much apples were eating in each round. At very high speed (~ every 1200 generated numbers) a pattern is visible in the graph. I can assure it repeats more than ~ 20 times, time when I just stop execution. I thought that it may be the work of ai, but I turned it off, resulting in every round snake going straight into a wall as I don't control it. What is random now is only the position of apple, so the graph shows how much time the apple was present in the sight line made from snake head and a wall (the wall is the same every round). Surprisingly the pattern still repeats. It is different at every launch though
Edit
Since I was told distributions are stateful I've update my code as follow:
std::random_device randomDevice;
struct Random {
Random(int min, int max) {
intD = new std::uniform_int_distribution<int>(min, max);
}
Random(float min, float max) {
realD = new std::uniform_real_distribution<float>(min, max);
}
~Random() {
delete engine;
delete intD;
delete realD;
}
int getI() {
return (*intD)(*engine);
}
float getR() {
return (*realD)(*engine);
}
std::mt19937* engine = new std::mt19937(randomDevice());
std::uniform_int_distribution<int>* intD = nullptr;
std::uniform_real_distribution<float>* realD = nullptr;
};
// Example
Random appleRandom(0, mapSize);
void random() {
T2<int> apple {appleRandom.getI(), appleRandom.getI()};
}
The problem still persists
Aucun commentaire:
Enregistrer un commentaire