这可能有助于:
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
int main()
{
double HI = 12345.67; // set HI and LO according to your problem.
double LO = 879.01;
double range= HI-LO;
MatrixXd m = MatrixXd::Random(3,3); // 3x3 Matrix filled with random numbers between (-1,1)
m = (m + MatrixXd::Constant(3,3,1.))*range/2.; // add 1 to the matrix to have values between 0 and 2; multiply with range/2
m = (m + MatrixXd::Constant(3,3,LO)); //set LO as the lower bound (offset)
cout << "m =\n" << m << endl;
}
输出:
m =
10513.2 10034.5 4722.9
5401.26 11332.6 9688.04
9858.54 3144.26 4064.16
所得矩阵将包含在类型和LO
HI
之间的范围内的双伪随机元素。
谢谢,这工作:) – pincir
一个更简单的一行表达将是:'M = LO +(ArrayXXd ::随机( M,N)* 0.5 + 0.5)*(HI-LO)' – ggael