C++ uniform_real_distribution及normal_distribution

C++ uniform_real_distribution及normal_distribution

std::uniform_real_distribution

std::uniform_int_distribution用於從均勻分布中生成隨機的整數;與它相對,std::uniform_real_distribution則是從均勻分布中生成隨機的浮點數。

其類別模板:

template <class RealType = double> class uniform_real_distribution;

我們可以透過指定其模板參數為doublefloat,以此來決定生成什麼精度的浮點數。

根據std::uniform_real_distribution::(constructor)std::uniform_real_distribution 的建構子的簽名如下:

std::uniform_real_distribution::(constructor)
/*(1)*/ explicit uniform_real_distribution ( result_type a = 0.0, result_type b = 1.0 );
/*(2)*/ explicit uniform_real_distribution ( const param_type& parm );

它的作用:

Construct uniform real distribution
Constructs a uniform_real_distribution object, adopting the distribution parameters specified either by a and b or by object parm.

即接受參數ab,分別表示取值範圍的下界(包含)跟上界(不包含),來生成一個uniform_real_distribution物件。

而根據std::uniform_real_distribution::operator()std::uniform_real_distribution::operator()的簽名如下:

/*(1)*/ template<class URNG> result_type operator()(URNG& g);
/*(2)*/ template<class URNG> result_type operator()(URNG& g, const param_type& parm);

它的作用為:

Generate random number
Returns a new random number that follows the distribution's parameters associated to the object (version 1) or those specified by parm (version 2).

The generator object (g) supplies uniformly-distributed random integers through its operator() member function. The uniform_real_distribution object transforms the values obtained this way so that successive calls to this member function with the same arguments produce floating-point values that follow a uniform distribution within the appropriate range.

即從均勻分布中隨機選取一個浮點數回傳,選取範圍的上下界是之前設定好的。

TensorRT/parsers/caffe/caffeWeightFactory/caffeWeightFactory.cpp 中定義的函數 allocateWeights(int64_t elems, std::uniform_real_distribution<float> distribution = std::uniform_real_distribution<float>(-0.01f, 0.01F))

//std::default_random_engine generator;
//nvinfer1::Weights allocateWeights(int64_t elems, std::uniform_real_distribution<float> distribution = std::uniform_real_distribution<float>(-0.01f, 0.01F));

Weights CaffeWeightFactory::allocateWeights(int64_t elems, std::uniform_real_distribution<float> distribution)
{
    //...
    ((float*) data)[i] = distribution(generator);
    //...
}

TensorRT/parsers/caffe/caffeParser/opParsers/parseInnerProduct.cpp 的函數 parseInnerProduct 中是這麼對它調用的:

/**/weightFactory.allocateWeights(nbOutputs))/**/;

std::normal_distribution

根據std::normal_distribution::(constructor)std::normal_distribution的建構子的簽名如下:

std::normal_distribution::(constructor)
/*(1)*/ explicit normal_distribution ( result_type mean = 0.0, result_type stddev = 1.0 );
/*(2)*/ explicit normal_distribution ( const param_type& parm );

它的作用:

Construct normal distribution
Constructs a normal_distribution object, adopting the distribution parameters specified either by mean and stddev or by object parm.

即接受參數meanstddev來建構一個normal_distribution物件。

而根據 std::normal_distribution::operator()std::normal_distribution::operator() 的簽名如下:

std::normal_distribution::operator()
/*(1)*/ template<class URNG> result_type operator()(URNG& g);
/*(2)*/ template<class URNG> result_type operator()(URNG& g, const param_type& parm);

它的作用如下:

Generate random number
Returns a new random number that follows the distribution's parameters associated to the object (version 1) or those specified by parm (version 2).

The generator object (g) supplies uniformly-distributed random integers through its operator() member function. The normal_distribution object transforms the values obtained this way so that successive calls to this member function with the same arguments produce floating-point values that follow a Normal distribution with the appropriate parameters.

std::normal_distribution::operator(URNG& g)會從正態分布中隨機選取一個數回傳,其平均值及標準差是之前設定好的。

TensorRT/parsers/caffe/caffeWeightFactory/caffeWeightFactory.cpp中定義的函數 allocateWeights(int64_t elems, std::normal_distribution<float> distribution)

//std::default_random_engine generator;
//nvinfer1::Weights allocateWeights(int64_t elems, std::normal_distribution<float> distribution);
Weights CaffeWeightFactory::allocateWeights(int64_t elems, std::normal_distribution<float> distribution)
{
    //...
    ((float*) data)[i] = distribution(generator);
    //...
}

TensorRT/parsers/caffe/caffeParser/opParsers/parseInnerProduct.cpp的函數parseInnerProduct中是這麼對它調用的:

/**/ weightFactory.allocateWeights(nbInputs * nbOutputs, std::normal_distribution<float>(0.0F, std_dev));

參考連結

std::uniform_real_distribution

std::uniform_real_distribution::(constructor)

std::uniform_real_distribution::operator()

std::normal_distribution::(constructor)

std::normal_distribution::operator()

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当谈到C++中的随机数面试题时,以下是一些常见的问题和答案: 问题1:如何在C++中生成随机数? 答案:在C++中,可以使用标准库中的`<random>`头文件来生成随机数。通常使用以下步骤: - 创建一个随机数引擎对象,如`std::default_random_engine`。 - 选择合适的随机数分布,如均匀分布`std::uniform_int_distribution`或正态分布`std::normal_distribution`。 - 使用随机数引擎和随机数分布来生成随机数。 问题2:如何生成一个范围在[min, max]之间的随机整数? 答案:可以使用`std::uniform_int_distribution`来实现。以下是一个示例代码: ```cpp #include <iostream> #include <random> int main() { int min = 1; int max = 10; std::random_device rd; std::default_random_engine engine(rd()); std::uniform_int_distribution<int> distribution(min, max); int randomNum = distribution(engine); std::cout << "Random number: " << randomNum << std::endl; return 0; } ``` 问题3:如何生成一个范围在[0, 1)之间的随机浮点数? 答案:可以使用`std::uniform_real_distribution`来实现。以下是一个示例代码: ```cpp #include <iostream> #include <random> int main() { std::random_device rd; std::default_random_engine engine(rd()); std::uniform_real_distribution<double> distribution(0.0, 1.0); double randomNum = distribution(engine); std::cout << "Random number: " << randomNum << std::endl; return 0; } ``` 这些是一些常见的C++随机数面试题,希望对你有所帮助!如有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值