Reputation: 92
I would like to use built-in distribution, but add some constraints to it. I tried something like this, but i get same number when using function. How can i avoid this? Can i use distribution as a argument to the function?
double Cauchy(double Fm){
std::default_random_engine generator;
std::cauchy_distribution<double> distribution(Fm, 0.1);
double number=distribution(generator);
while(number<0)
number=distribution(generator);
if (number>1)
number = 1;
return number;
}
Now i changed function and it looks like this
double Cauchy_1(double Fm, std::random_device &rd){
std::default_random_engine generator(rd());
std::cauchy_distribution<double> distribution(Fm, 0.1);
double number=distribution(generator);
while(number<0)
number=distribution(generator);
if (number>1)
number =1;
return number;
}
std::random_device rd;
int i=15;
double Crm=0.1, num;
while (i>0){
num=Cauchy_1(0.1, rd);
cout<<num<<endl;
i--;
}
It gives me different values, but values are the same on new run.
Upvotes: 2
Views: 283
Reputation: 26342
Functions in the standard library like std::shuffle
take a random number generator by a forwarding reference, not a distribution. You can do the same:
template<class URBG>
double cauchy_1(double fm, URBG&& gen) {
std::cauchy_distribution<double> distribution(fm, 0.1);
double number;
do
number = distribution(gen);
while (number < 0);
return std::min(number, 1.0);
}
int main() {
std::random_device rd;
std::default_random_engine gen(rd());
for (int i = 0; i < 10; ++i) {
auto num = cauchy_1(0.1, gen);
std::cout << num << std::endl;
}
}
It still has same set of numbers if i rerun this code.
This is not the problem of this code, but the problem of std::random_device
. As explained here, std::random_device
may be implemented in terms of a pseudo-random number engine. Possible solutions can be found here.
std::default_random_engine gen(
std::chrono::system_clock::now().time_since_epoch().count());
Upvotes: 3
Reputation: 4713
You initialize std::default_random_engine generator;
with the same default seed. You need to seed it to get different outputs if you instantiate it anew. There is std::random_device
class you can use to get a new random seed.
Also, std::default_random_engine
is slow class to instantiate/create so you are using it wrong.
Upvotes: 4