MathSrand

建立伪随机整数产生序列出发点。

void  MathSrand(
   int  seed      // 初始化数字
   );

参量

seed

[in]  随机数开始数字的排列。

返回值

没有返回值。

注意

MathRand() 函数用于生成一系列的伪随机数。通过正在初始化的数字调用MathSrand() 允许始终生产同系列的伪随机数。

要确定收到非循环数列,使用调用MathSrand(GetTickCount()),由于GetTickCount() 值从操作系统开始时增加并且在49天内不重复,一直到超出内置毫秒计数器。使用MathSrand(TimeCurrent()) 不合适,因为TimeCurrent() 函数返回长时间未更改的最近订单号的时间,例如周末。

使用MathSrand()函数为指标和EA而初始化的随机数生成程序在 OnInit()处理程序被更好的执行,它可以有助于您防止以下在OnTick() 和 OnCalculate()多次重启生成程序。

您可以使用srand()函数来替代MathSrand()函数。

例如:

#property description "The indicator shows the central limit theorem, which states:"
#property description "The sum of a sufficiently large number of weakly dependent random variables, "
#property description "having approximately equal magnitude (none of the summands dominates,"
#property description "or makes a determining contribution to the sum), has a distribution close to normal."
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- 图形结构属性
#property indicator_label1  "Label"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrRoyalBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  5
//--- 输入变量
input int      sample_number=10;
//--- 用于绘制分布图的指标缓冲区
double         LabelBuffer[];
//--- 订单号计数器
double         ticks_counter;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数                                                |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- 绑定数组和指标缓冲区
   SetIndexBuffer(0,LabelBuffer,INDICATOR_DATA);
//--- 从现在到过去转移指标缓冲区
   ArraySetAsSeries(LabelBuffer,true);
//--- 初始化随机数生成程序
   MathSrand(GetTickCount());
//--- 初始化订单号计数器
   ticks_counter=0;
  }
//+------------------------------------------------------------------+
//| 自定义指标迭代函数                                                 |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- 为零值计数器重置指标缓冲区
   if(ticks_counter==0) ArrayInitialize(LabelBuffer,0);
//--- 增加计数器
   ticks_counter++;
//--- 我们应该周期性的重置计数器,恢复分配
   if(ticks_counter>100)
     {
      Print("We've reset the indicator values, let's start filling the cells once again");
      ticks_counter=0;
     }
//--- 得到从0到7三个数字总和的随机值样本
   for(int i=0;i<sample_number;i++)
     {
      //--- 计算单元标引,随机数下跌三个数字的总和
      int rand_index=0;
      //--- 得到从0到7的三个随机数字
      for(int k=0;k<3;k++)
        {
         //--- 除以7的余数将返回从0到6的值
         rand_index+=MathRand()%7;
        }
      //--- 增加rand_index单元数的值为1
      LabelBuffer[rand_index]++;
     }
//--- 退出OnCalculate() 处理程序
   return(rates_total);
  }