Need help with generating random fromseed in my class
Hello.
I am trying to write class for generating random floats.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
template<unsigned short seed>
class PseudoRandomGenerator {
public:
PseudoRandomGenerator();
std::default_random_engine generator (seed);
std::uniform_real_distribution<float> distribution;
float operator()() {
return distribution(generator);
}
};
|
However C++ is not happy:
1 2 3
|
error: ‘seed’ is not a type
std::default_random_engine generator (seed);
^
|
I would like to know how I could fix this?
Last edited on
Initialize the generator in the constructor.
1 2 3 4 5
|
template<unsigned short seed>
PseudoRandomGenerator<seed>::PseudoRandomGenerator()
: generator(seed)
{
}
|
Last edited on
Alternately:
1 2 3 4 5 6 7 8 9 10 11
|
template<unsigned short seed>
class PseudoRandomGenerator {
public:
std::default_random_engine generator = std::default_random_engine(seed);
std::uniform_real_distribution<float> distribution;
float operator()() {
return distribution(generator);
}
};
|
Although, why you would opt for the constraint that the seed must be a compile-time constant is a little puzzling. Not a good template candidate.
Topic archived. No new replies allowed.