简体   繁体   English

将 C++ 随机数分布传递给函数

[英]pass C++ random number distribution to a function

I have looked for similar questions but haven't found them.我一直在寻找类似的问题,但没有找到。 I want to generate normally distributed random numbers.我想生成正态分布的随机数。 I used to code C and some C++98 but am now trying to go back and learn C++11.我曾经编写过 C 和一些 C++98 代码,但现在我正在尝试回去学习 C++11。

I have a function to return a seeded RNG我有一个返回种子 RNG 的函数

auto seeded_rng () {
      ....  //do seeding.
     std::default_random_engine Eng(/*seeds*/);
     return Eng;
}

In my main function I bind the RNG to say a gaussian distribution在我的主函数中,我将 RNG 绑定为高斯分布

auto binded = std::bind(std::normal_distribution<double>{0,1.0},seeded_rng);

This function works fine.此功能工作正常。 I can call "binded()" directly in main and it generates the numbers我可以直接在 main 中调用“bind()”并生成数字

I want to have a simulation object that needs random numbers to be created.我想要一个需要创建随机数的模拟对象。 My question related to how to pass in the "RNG_PART" below.我的问题与如何传入下面的“RNG_PART”有关。

class sim
{
public:
       sim( RNG_PART & rng, int_number_of sims ){ /* Do whatever */}
}

So if in main, I then want to create a simulation object所以如果在main中,我想创建一个模拟对象

sim A(binded, 100);

it complains.它抱怨。 I tried declaring我试着声明

sim::sim(std::default_random_engine &rng, int number_of_sims){}

but it is complaining.但它在抱怨。 What type should I use to pass in the "binded" distribution to the constructor?我应该使用什么类型将“绑定”分布传递给构造函数? Or am I going about this completely incorrectly.或者我完全错误地处理这个问题。 Should I just declare the RNG engine globally?我应该在全球范围内声明 RNG 引擎吗? I'd prefer not to do that.我宁愿不这样做。

Apologies if this is very basic!抱歉,如果这是非常基本的!

The type of the argument to sim is not matching the type of binded. sim 参数的类型与绑定的类型不匹配。 When you create binded, you avoid the issue by using auto instead of declaring the type, but you're going to need it later.当您创建 binded 时,您可以通过使用auto而不是声明类型来避免该问题,但您稍后将需要它。 How about the following in class sim instead of trying to figure out the type ahead of time.模拟课程中的以下内容如何,​​而不是试图提前弄清楚类型。 This also allows you to change the RNG or the random distribution without changing the sim class:这也允许您在不更改 sim 类的情况下更改 RNG 或随机分布:

template<typename T>
class sim
{    
public:
    sim(T& rng, int_number_of sims ){ /* Do whatever */}

Note that the template definition must be visible to the place you use it, so you can't put it into a cpp file unless the only code that uses it is in that file.请注意,模板定义必须在您使用它的地方可见,因此您不能将其放入 cpp 文件,除非使用它的唯一代码在该文件中。 Typically, the template definition is in the same h file as where it was declared.通常,模板定义与声明它的位置在同一个 h 文件中。

Then, you'd create sim as:然后,您将创建 sim 为:

sim<decltype(binded)> A(binded,100);

the decltype(binded) is a way to tell the sim function template the type of binded. decltype(binded)是一种告诉 sim 函数模板绑定类型的方法。

Since binded can return different types depending upon the chosen random number distribution, obtaining the return type in the sim class template could be done with由于 binded 可以根据选择的随机数分布返回不同的类型,因此可以通过以下方式获取 sim 类模板中的返回类型

using rnd_return_type = typename std::result_of<T()>::type; //in C++11, C++14

or或者

using rnd_return_type = std::invoke_result_t<T>; //C++17 and later

std::result_of is deprecated in C++17 and will be removed in C++20 std::result_of在 C++17 中被弃用,并将在 C++20 中移除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM