Random

静态函数。创建并返回一个填充了随机值的新矩阵或向量。随机值在指定范围内均匀生成。

static vector vector::Random(
  const ulong   size,       // 向量长度
  const double  min=0.0,    // 最小值
  const double  max=1.0     // 最大值
   );
 
static matrix matrix::Random(
  const ulong   rows,       //行数
  const ulong   cols        // 列数
  const float   min=0.0,    // 最小值
  const float   max=1.0     // 最大值
   );
 

该函数用随机值填充现有的矩阵或向量。随机值在指定范围内均匀生成。

void  vector::Random(
  const double  min=0.0,    // 最小值
  const double  max=1.0     // 最大值
   );
 
void  matrix::Random(
  const float  min=0.0,     // 最小值
  const float  max=1.0      // 最大值
   );

参数

rows

[in]  行数。

cols

[in]  列数。

size

[in]  向量长度。

min=0.0

[in] 生成的随机数样本中的最小值。

max=1.0

[in] 生成的随机数样本中的最大值。

返回值

返回一个由给定行和列组成的新矩阵,其中填充了随机值。

示例

//+------------------------------------------------------------------+
//| 脚本程序起始函数                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   vector v1vector::Random(6,0,4);
   Print("vector v1 \n"v1);  
   /*
   vector v1 
   [3.340834286841758,0.9073984578501895,3.15598511117417,0.7106475841045956,2.294010032502471,0.2469535936245121]
   */
      
   vector v2(4);
   v2.Random(-3,3);
   Print("vector v2 \n"v2);      
   /*
   vector v2 
   [-2.642032080246609,1.008521607147651,1.340623848219547,-2.789016363161853]
   */
 
   matrix m1 = matrix::Random(55, -13);
   Print("matrix m1 \n"m1);
   /*
   matrix m1 
   [[1.468433369525809,1.199383023707052,0.6154667729547869,2.830012157519816,-0.06995551732134897]
    [2.638591175814763,2.966658948912904,2.001837257994767,-0.08570802469870731,0.8260628995080626]
    [0.982665140643405,0.5825661973987162,-0.6615881071569371,0.4069533109215444,-0.1612716566681097]
    [-0.5791836119208447,-0.9332020478167575,2.801898707589577,-0.09887318478405316,-0.3206291206461911]
    [-0.1069569017521471,-0.04576879345014895,0.6320100840255642,-0.3805729270401813,0.7021634554728315]]
   */
 
   matrix m2(33);
   m2.Random(-22);
   Print("matrix m2 \n"m2);
   /*
   matrix m2 
   [[-1.928191291151613,-1.222873445419332,-1.965333905062949]
    [-1.077723600686929,1.053317863273741,-0.1786824246353196]
    [-1.965241401448204,1.620956906029246,-1.696386492740453]]
   */   
  }