1
General / Re: All sprites in vector are moving in the exact same pattern
« on: February 26, 2020, 09:57:43 am »
It's because you don't understand how the random functions are intended to be used. They're quite over-complicated for simple uses because they're intended to be useful for scientists and statisticians.
The previous poster is also wrong, you shouldn't keep reseeding every call, as if you call more than once in a second you'll keep getting the same result (by design)!
Consider the following code snippet, similar to yours:
The output is:
iteration #0
3
3
iteration #1
1
1
iteration #2
0
0
This is because we seed two random generators with the same seed at the same time, then call them at the same times! You should only seed a random generator once in your entire application, then call it everywhere, unless you have very exotic needs.
Put it in a static method with a lazy initializer would be my recommendation, like:
And call it like this (anywhere):
Good luck.
The previous poster is also wrong, you shouldn't keep reseeding every call, as if you call more than once in a second you'll keep getting the same result (by design)!
Consider the following code snippet, similar to yours:
// Example program
#include <iostream>
#include <string>
#include <random>
class Test
{
private:
std::mt19937 mt;
std::uniform_int_distribution<int> dist;
public:
Test() : mt(time(0)), dist(0, 4)
{
}
int genRandom()
{
return dist(mt);
}
};
int main()
{
Test t1{};
Test t2{};
for(int i = 0; i < 3; i++)
{
std::cout << "iteration #" << i << "\n";
std::cout << t1.genRandom() << "\n";
std::cout << t2.genRandom() << "\n";
}
}
#include <iostream>
#include <string>
#include <random>
class Test
{
private:
std::mt19937 mt;
std::uniform_int_distribution<int> dist;
public:
Test() : mt(time(0)), dist(0, 4)
{
}
int genRandom()
{
return dist(mt);
}
};
int main()
{
Test t1{};
Test t2{};
for(int i = 0; i < 3; i++)
{
std::cout << "iteration #" << i << "\n";
std::cout << t1.genRandom() << "\n";
std::cout << t2.genRandom() << "\n";
}
}
The output is:
iteration #0
3
3
iteration #1
1
1
iteration #2
0
0
This is because we seed two random generators with the same seed at the same time, then call them at the same times! You should only seed a random generator once in your entire application, then call it everywhere, unless you have very exotic needs.
Put it in a static method with a lazy initializer would be my recommendation, like:
class Random
{
public:
static int getRandomInt(int min, int max)
{
// static so it's only created once, then every other call reuses it (forever)
// if you need multiple types of random functions you'd need to make it a private member
// and reuse it instead
static std::mt19937 mt(time(0));
auto dist = std::uniform_int_distribution<int>(min, max);
return dist(mt);
}
};
{
public:
static int getRandomInt(int min, int max)
{
// static so it's only created once, then every other call reuses it (forever)
// if you need multiple types of random functions you'd need to make it a private member
// and reuse it instead
static std::mt19937 mt(time(0));
auto dist = std::uniform_int_distribution<int>(min, max);
return dist(mt);
}
};
And call it like this (anywhere):
Random::getRandomInt(0, 4)
Good luck.