【问题标题】:Eigen template library Random() method algorithm特征模板库 Random() 方法算法
【发布时间】:2015-06-15 10:05:15
【问题描述】:

有谁知道,C++ 的 Eigen 库在方法 Random() 中使用了哪种算法:

eigen - random() doc

?

如果 eigen 将此决定传递给编译器(使用其标准方法创建伪随机数),那么我想知道 g++ (gcc49 4.9.2_2) 默认使用哪种算法。

非常感谢任何有用的提示。

【问题讨论】:

  • 通过 Eigen 源的快速 grep 似乎表明它只是调用 rand(),因此您需要查看您的编译器库。

标签: c++ random eigen


【解决方案1】:

正如 Paul R 所指出的,默认情况下只调用 rand。如果你查看 Eigen/src/Core/MathFunctions.h 你会发现默认值:

template<typename Scalar>
struct random_default_impl<Scalar, false, false>
{
  static inline Scalar run(const Scalar& x, const Scalar& y)
  {
    return x + (y-x) * Scalar(std::rand()) / Scalar(RAND_MAX);
  }
  static inline Scalar run()
  {
    return run(Scalar(NumTraits<Scalar>::IsSigned ? -1 : 0), Scalar(1));
  }
};

或主题的变体(针对不同的变量类型)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-16
    • 2020-04-12
    • 1970-01-01
    • 2020-12-24
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多