Hello everyone,
I wanted to ask for an advice about how to create .cpp file (new class) that will handle all the random number generators (<random> library) that main could possibly access. When I looked for a way how to implement random generators the examples were always in the main function or in a class in the main.cpp (with no header file and the generator was not seeded as I imagine it should be = only once in a program).
Questions follows after this malfunction code.
main.cpp
1 2 3 4 5 6 7
|
#include "randomizer.h"
int main()
{
Randomizer randObject(0, 10, 125); //Set bounds and seed generator
int mainVar = randObject.getRandInt(); //Store newly generated random number for future work
}
|
randomizer.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <random> //All random generators
#include <ctime> //std::time
class Randomizer
{
public:
Randomizer(int boundMin,
int bountMax,
int seed = std::time(0));
//~Randomizer(); //Destructor not required?
int getRandInt();
int getSeed();
private:
int seed_; //Store seed value if needed later
//Here should be probably declaration of generator/distribution, but how?
}
|
randomizer.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include "randomizer.h"
Randomizer::Randomizer(int boundMin, int boundMax, int seed)
: seed_(seed)
{
std::default_random_engine defGen(seed_);
std::uniform_int_distribution<int> randInt(boundMin, boundMax);
}
int Randomizer::getRandInt()
{
return randInt(defGen); //Doesn't work
//randInt probably went out of bounds at the end of Randomizer()
}
int Randomizer::getSeed()
{
return seed_;
}
|
Now when I tried to change function getRandInt to:
1 2 3 4 5 6
|
int Randomizer::getRandInt()
{
std::uniform_int_distribution<int> randInt(boundMin_, boundMax_);
//boundMin_, boundMax_ were declared as private class variables earlier
return randInt(defGen); //Doesn't work - seed is not changing rand output
}
|
1) So I think that I should somehow declare these generators and distribution in the header file. But I have no idea how. Is it like declaring
int a;
?
I tried writing
std::default_random_engine defGen;
into the header file which only silenced compiler's errors but
defGen
wasn't seeded.
I have seen some examples using
auto a = randInt(defGen);
(or maybe with the use of
auto a = std::bind...
. But what does
auto
represent? I can't use it in the header file.
2) Can I ensure that the
randObject
is seeded only once? If for examle I need
randObject2
with different
boundMax
, can I still use the same
defGen
which was seeded the first time? That should ensure better "randomness" through the program, shouldn't it?
3) Is there better way (and easy too) to "interface" random number generators? The class in the end should provide "get" function to acces
int, double, bool, signed/unsigned...
or some other specialities like random prime numbers, etc.
If it is anyhow related I am using Code::Blocks IDE with GCC compiler (4.7.1)
I would be grateful for any help :-)